启用布尔值并在视图中输入文本然后传回控制器 - MVC

cg9*_*g91 5 c# checkbox asp.net-mvc razor

我有一个视图,在视图中以框格式显示一个布尔值(当前默认为0),我无法检查激活为true,还想在结果字段中输入文本以传回控制器并将两个更改保存到一张桌子.有人可以解释我必须做什么才能让这个功能工作.

在此输入图像描述

控制器代码

public ActionResult P1A1Mark()
    {
        List<MarkModel> query = (from row in db.submits
                                     where row.assignment_no.Equals("1") && row.group_no == 1
                                     group row by new { row.assignment_no, row.student_no, row.student.firstname, row.student.surname } into g
                                     select new MarkModel
                                     {
                                         student_no = g.Key.student_no,
                                         student_surname = g.Key.surname,
                                         student_firstname = g.Key.firstname

                                     }
                                        ).ToList();

        return View(query);
    }
Run Code Online (Sandbox Code Playgroud)

视图

@model IEnumerable<MvcApplication2.Models.MarkModel>

@{
    ViewBag.Title = "P1A1Mark";
}

<h2>Mark Student Assignments</h2>

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.student_no)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.student_surname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.student_firstname)
        </th>
         <th>
            @Html.DisplayNameFor(model => model.submitted)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.result)
        </th>

        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.student_no)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.student_surname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.student_firstname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.submitted)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.result)
        </td>

    </tr>
}

</table>
Run Code Online (Sandbox Code Playgroud)

模型

    public class MarkModel
{
    public string student_no { get; set; }
    public string student_surname { get; set; }
    public string student_firstname { get; set; }
    public string assignment_no { get; set; }
    public bool submitted { get; set; }
    public string result { get; set; }
    public Nullable<int> group_no { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

小智 1

创建一个EditorTemplatefor 类型MarkModel

/Views/Shared/EditorTemplates/MarkModel.cshtml

@model MvcApplication2.Models.MarkModel
<tr>
  <td>@Html.DisplayFor(m => m.student_no)</td>
  <td>@Html.DisplayFor(m => m.student_surname)</td>
  <td>@Html.DisplayFor(m => m.student_firstname)</td>
  <td>@Html.CheckBoxFor(m => m.submitted)</td>
  <td>@Html.TextBoxFor(m => m.result)</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

并在主视图中

@model IEnumerable<MvcApplication2.Models.MarkModel>
@using (Html.BeginForm())
{
  <table>
    <thead>
      // add your th elements
    </thead>
    <tbody>
      @Html.EditorFor(m => m)
    <tbody>
  </table>
  <input type="submit" ../>
}
Run Code Online (Sandbox Code Playgroud)

并创建一个方法来回发

[HttpPost]
public ActionResult P1A1Mark(IEnumerable<MarkModel>model)
Run Code Online (Sandbox Code Playgroud)

或者,您可以在视图中使用for循环(模型必须是IList<T>

for(int i = 0; i < Model.Count; i++)
{
  ....
  @Html.CheckBoxFor(m => m[i].submitted)
}
Run Code Online (Sandbox Code Playgroud)