0

我正在尝试通过单击提交按钮保存多个学生的课堂出勤率。我能够在关注表中创建空白记录,然后在视图中填充数据。

我有以下视图模型:

public class TeacherAttendanceModel
{
    #region Required Properties

        public long ScholarAttendanceId { get; set; }
        public string Student { get; set; }
        public bool Absent { get; set; }
        public string AbsentComment { get; set; }
        public bool Uniform { get; set; }
        public bool Homework { get; set; }
        public string HomeworkComment { get; set; }
        public String UniformCommentSelected { get; set; }
        public IEnumerable<String> UniformComment { get; set; }

    #endregion

}

我的控制器如下。

public class TeacherAttendanceController : Controller
{
    //
    // GET: /TeacherAttendance/

    public ActionResult Index()
    {
        long classId = Success.Business.Roles.Teacher.GetHomeRoomClassID(Convert.ToInt64(Session[GlobalVar.LOGGED_IN_ID]));
        var classAttendanceStatus = Success.Business.Entities.ClassAttendance.GetClassAttendanceStatus(classId);
        ViewBag.status = classAttendanceStatus;
        var attendanceData = TeacherAttendance.CreateClassAttendance(classId);
        return View(attendanceData);
    }

    [HttpPost]
    public ActionResult Index(IEnumerable<TeacherAttendanceModel> teacherAttendanceModel)
    {
        try
        {
            if (ModelState.IsValid)
            {
                TeacherAttendance.SaveAttendance(teacherAttendanceModel);
            }
        }
        catch (Exception e)
        {

        }

        return View(teacherAttendanceModel);
    }

}

获取索引工作正常。但我没有在 Post 索引中获得 TeachAttendanceModel 对象。我得到空对象。我将非常感谢在这方面获得任何帮助。提交点击如何更新多条考勤记录?

我正在使用以下视图:

@foreach (var item in Model) {
    <tr >
        <td style="border-style:solid; border-color:darkslategray; border-width:thin;">
            @Html.DisplayFor(modelItem => item.Student)
        </td>
        <td style="border-style:solid; border-color:darkslategray; border-width:thin;">
            @Html.CheckBoxFor(modelItem => item.Absent, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
            @Html.TextBoxFor(modelItem => item.AbsentComment, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
        </td>
        <td style="border-style:solid; border-color:darkslategray; border-width:thin;">
            @Html.CheckBoxFor(modelItem => item.Uniform, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
            @Html.DropDownListFor(modelItem => item.UniformCommentSelected, new SelectList(item.UniformComment),item.UniformCommentSelected ?? "---Select---", ViewBag.status == 2? new {disabled = "disabled"} : null)
        </td>
        <td style="border-style:solid; border-color:darkslategray; border-width:thin;">
            @Html.CheckBoxFor(modelItem => item.Homework, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
            @Html.TextBoxFor(modelItem => item.HomeworkComment, ViewBag.status == 2? new {disabled = "disabled"} : null)
        </td>

    </tr>
}
4

2 回答 2

1

模型:

public class Test
{
    public List<string> UniformComment { get; set; }
}

控制器:

    public ActionResult Index()
    {
        var model = new Test
            {
                UniformComment = new List<string>{ "one", "two", "three" }
            };

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(Test model)
    {
        return View(model);
    }

看法:

@using (Html.BeginForm())
{
    for (var i = 0; i < Model.UniformComment.Count; i++)
    {
        @Html.TextBoxFor(x => Model.UniformComment[i])
    }

    <input type="submit" value="Save" />
}

呈现的 html 示例:

<input id="UniformComment_0_" name="UniformComment[0]" type="text" value="one" />
<input id="UniformComment_1_" name="UniformComment[1]" type="text" value="two" />
<input id="UniformComment_2_" name="UniformComment[2]" type="text" value="three" />

这个想法是使用for循环或创建进行迭代,EditorTemplate然后您会收到索引项目。

补充(感受不同):

看法:

@using (Html.BeginForm())
{
    foreach (var comment in Model.UniformComment)
    {
        @Html.TextBoxFor(x => comment)
    }

    <input type="submit" value="Save" />
}

呈现的html:

<input id="comment" name="comment" type="text" value="one" />
<input id="comment" name="comment" type="text" value="two" />
<input id="comment" name="comment" type="text" value="three" />
于 2012-10-07T12:29:47.233 回答
0

在视图中使用 IList 而不是 IEnumerable,并将 foreach 循环替换为 for 循环。

步骤1:

利用

@model IList<TeacherAttendanceModel>

代替

@model IEnumerable<TeacherAttendanceModel>

第2步:

利用

@for (var i = 0; i < Model.Count; i++) 

代替

@foreach (var item in Model)

请参阅如何将 IEnumerable 列表传递给 MVC 中的控制器,包括复选框状态?更多细节。

于 2013-08-06T09:57:43.747 回答