c# - 将 IPagedList 与复选框绑定(bind)为列表

标签 c# asp.net-mvc

所以.. 我有一个类有一个 List。 我像下面的代码一样将它传递给 View :

[HttpGet]
[Authorize(Roles="user")]
[CustomChecker]
public ActionResult Index(int? page, int id=0)
{
    EmployeeContext emp = new EmployeeContext();
    student st = emp.students.Single(x=>x.id ==id);

    @ViewBag.id = st.id;

    return View(st.subjSel.ToPagedList(page ?? 1, 4));
}

然后 View 会像这样接收它:

@using PagedList;
@using PagedList.Mvc;
@model PagedList<MvcApplication6.Models.subject>
<div style="font-family:Arial">
    <fieldset>
        <legend><h3>Open Classes</h3></legend>
        @using (Html.BeginForm("Test", "Enrollment"))
        {
            <input type="hidden" name="id" value="@ViewBag.id" />
            <table border="1">
                <tr>
                    <th>@Html.LabelFor(model => model[0].subj)</th>
                    <th>@Html.LabelFor(model => model[0].days)</th>
                    <th>@Html.LabelFor(model => model[0].cstart)</th>
                    <th>@Html.LabelFor(model => model[0].cend)</th>
                    <th>@Html.LabelFor(model => model[0].professor)</th>
                    <th>@Html.LabelFor(model => model[0].units)</th>
                    <th>@Html.CheckBox("test") Select all</th>
                </tr>
                @for (int i = 0; i < Model.Count; i++)
                {
                    <tr>
                        @Html.HiddenFor(model => model[i].id)
                        <td>
                            @Html.DisplayFor(m => m[i].subj)
                            @Html.HiddenFor(m => m[i].subj)
                        </td>
                        <td>
                            @Html.DisplayFor(m => m[i].days)
                            @Html.HiddenFor(m => m[i].days)
                        </td>
                        <td>
                            @Html.DisplayFor(m => m[i].cstart)
                            @Html.HiddenFor(m => m[i].cstart)
                        </td>
                        <td>
                            @Html.DisplayFor(m => m[i].cend)
                            @Html.HiddenFor(m => m[i].cend)
                        </td>
                        <td>
                            @Html.DisplayFor(m => m[i].professor)
                            @Html.HiddenFor(m => m[i].professor)
                        </td>
                        <td>
                            @Html.DisplayFor(m => m[i].units)
                            @Html.HiddenFor(m => m[i].units)
                        </td>
                        <td>
                            @Html.CheckBoxFor(m => m[i].isSelected)
                        </td>
                    </tr>
                }
            </table>
            <br />
            <br />
            <table>
                <tr><td align="center" width="500px"></td></tr>
                 <tr>
                    <td align="center" width="500px">
                        <input type="submit" value="submit" /> | <input type="button" value="clear" />
                    </td>
                </tr>
            </table>
            <br />
            <br />
        }
    </fieldset>
</div>
@Html.PagedListPager(Model, page => Url.Action("Index", "Enrollment", new { page, id = Request.QueryString["id"] }))

我的问题是它会像这样呈现 [0].subj 并且不允许我绑定(bind),因为它应该像 name[0].subj

我一直在试验和尝试新的方法,有什么方法可以让我正确地绑定(bind)它们吗?我想尽可能多地使用 Html Helpers,我不想仅为这部分重新实现一个自定义的。

这是他们应该绑定(bind)的函数。这个类(class)有一个List学生(我转换成IPagedList的那个)

[HttpPost]
[Authorize(Roles="user")]
public ActionResult Test(student st)

这就是我的 View 的样子。我正在使用 CheckBoxFor 进行选择。 enter image description here

附加问题: 我的导航怎么这么难看?

最佳答案

模型是 View 是@model PagedList<subject>这意味着参数必须是

[HttpPost]
public ActionResult Test(IEnumerable<subject> model)

如果你也需要学生ID属性,然后包含一个附加参数

[HttpPost]
public ActionResult Test(IEnumerable<subject> model, int ID)

因为你的 GET 方法有一个参数 int ID并假设您使用默认路由,那么 ID 将添加到表单中 action属性,即它将呈现 action="/Enrollment/Test/2"假设 ID 的值为 2 .如果没有,您可以将其添加为路由参数

@using (Html.BeginForm("Test", "Enrollment", new { ID = ViewBag.id }))

或者你可以使用 View 模型

public class StudentVM
{
  public int ID { get; set; }
  public PagedList<student> Students { get; set; }
}

在 GET 方法中

public ActionResult Index(int? page, int id=0)
{
  EmployeeContext emp = new EmployeeContext();
  student st = emp.students.Single(x=>x.id ==id);
  StudentVM model = new StudentVM()
  {
    ID = id,
    Students = st.subjSel.ToPagedList(page ?? 1, 4)
  };
  return View(model);
}

然后将您的 View 基于 View 模型并将其发回给 public ActionResult Test(StudentVM model)

关于c# - 将 IPagedList 与复选框绑定(bind)为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32559113/

相关文章:

jquery - L 值是预期的 javascript

c# - 如何在 xaml 中裁剪图像并限制图像宽度? (温特)

c# - 在 CommandParameter 中传递项目 DataContext

c# - Linq 到 SQL : Where clause comparing a Nullable<DateTime> with a SQL datetime null column

c# - EF6 SQLQuery 非常慢,但数据库非常快

javascript - jquery ajax成功回调难度

C# 如果字符串包含超过 1 个值

asp.net-mvc - 如何在 MVC 4 操作中的冗长批处理过程中返回状态信息?

asp.net-mvc - MVC 6 VNext如何设置HtmlFieldPrefix?

javascript - 突出显示/取消突出显示列中的一行