Asp.net mvc 4 在提交时保存多条记录单击MVC4中的不同实体。无法从控制器中的视图获取值

Asp.net mvc 4 在提交时保存多条记录单击MVC4中的不同实体。无法从控制器中的视图获取值,asp.net-mvc-4,Asp.net Mvc 4,我试图通过点击提交按钮保存多个学生的课堂出勤情况。我可以在关联表中创建空白记录,然后在视图中填充数据。 我有以下视图模型: public class TeacherAttendanceModel { #region Required Properties public long ScholarAttendanceId { get; set; } public string Student { get; set; } public bool

我试图通过点击提交按钮保存多个学生的课堂出勤情况。我可以在关联表中创建空白记录,然后在视图中填充数据。 我有以下视图模型:

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 TeacherAttendanceModel
{
#区域所需属性
公共长期学术活动{get;set;}
公共字符串Student{get;set;}
公共布尔缺席{get;set;}
公共字符串AbsentComment{get;set;}
公共布尔一致{get;set;}
公共bool作业{get;set;}
公共字符串HomeworkComment{get;set;}
公共字符串UniformCommentSelected{get;set;}
public IEnumerable UniformComment{get;set;}
#端区
}
我的控制器如下

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);
    }

}
公共类教师AttendanceController:控制器
{
//
//获得教师出勤率/
公共行动结果索引()
{
long classId=Success.Business.Roles.Teacher.GetHomeRoomClassID(Convert.ToInt64(会话[GlobalVar.LOGGED\u ID]);
var classAttendanceStatus=Success.Business.Entities.ClassAttendance.GetClassAttendanceStatus(classId);
ViewBag.status=classAttendanceStatus;
var attendanceData=教师出勤率。创建课堂出勤率(classId);
返回视图(attendanceData);
}
[HttpPost]
公共行动结果索引(IEnumerable teacherAttendanceModel)
{
尝试
{
if(ModelState.IsValid)
{
TeacherAttention.SaveAttention(TeacherAttentindaceModel);
}
}
捕获(例外e)
{
}
返回视图(teacherAttendanceModel);
}
}
Get索引工作正常。但是我没有在Post索引中得到teachatendancemodel对象。我得到空对象。如果能在这方面得到任何帮助,我将不胜感激。如何在单击提交时更新多条考勤记录

我使用以下观点:

@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>
}
@foreach(模型中的变量项){
@DisplayFor(modeleItem=>item.Student)
@CheckBoxFor(modelItem=>item.缺席,ViewBag.status==2?新建{disabled=“disabled”}:null)
@Html.TextBoxFor(modelItem=>item.AbsentComment,ViewBag.status==2?新建{disabled=“disabled”}:null)
@Html.CheckBoxFor(modelItem=>item.Uniform,ViewBag.status==2?新建{disabled=“disabled”}:null)
@Html.DropDownListFor(modelItem=>item.UniformCommentSelected,new SelectList(item.UniformComment),item.UniformCommentSelected”----选择----,ViewBag.status==2?新建{disabled=“disabled”}:空)
@Html.CheckBoxFor(modelItem=>item.Homework,ViewBag.status==2?新建{disabled=“disabled”}:null)
@Html.TextBoxFor(modelItem=>item.HomeworkComment,ViewBag.status==2?新建{disabled=“disabled”}:null)
}
型号:

public class Test
{
    public List<string> UniformComment { get; set; }
}
呈现的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" />

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

第1步:

使用


有关更多详细信息,请参阅。

我在视图中使用for循环,并能够在下拉列表中绑定数据。启用禁用我使用的基于状态打开和提交它将显示。
@using (Html.BeginForm())
{
    for (var i = 0; i < Model.UniformComment.Count; i++)
    {
        @Html.TextBoxFor(x => Model.UniformComment[i])
    }

    <input type="submit" value="Save" />
}
<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" />
@using (Html.BeginForm())
{
    foreach (var comment in Model.UniformComment)
    {
        @Html.TextBoxFor(x => comment)
    }

    <input type="submit" value="Save" />
}
<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" />
@model IList<TeacherAttendanceModel>
@model IEnumerable<TeacherAttendanceModel>
@for (var i = 0; i < Model.Count; i++) 
@foreach (var item in Model)