【问题标题】:CheckBox List item not posting in mvc 5复选框列表项未在 mvc 5 中发布
【发布时间】:2017-12-21 00:29:22
【问题描述】:

我正在为特定批次注册的学生制作出勤面板。为此,我根据为该批次分配的课程数量显示学生的记录以及复选框的数量。一切都正确显示,但是只有一行的复选框将值带到帖子中,而其他行中的其余复选框不会发布。每行的学生详细信息都在列表中发布更正。

下面是我的代码

StudentAttendance.cs

public class StudentAttendance
    {
        public List<Models.User> userlist { get; set; }
        public List<Models.Days> days { get; set; }
    }

InstructorController.cs

public ActionResult AssignedStudents(string id)
        {
            Models.StudentAttendance viewmodel = new Models.StudentAttendance();
            //viewmodel.studentbatch = new Models.StudentBatch();
            //viewmodel.user = new Models.User();

            Context.Instructor instructor = new Context.Instructor();

            viewmodel.userlist = new List<Models.User>();
            viewmodel.days = new List<Models.Days>();
            viewmodel.userlist = instructor.lstAssignedStudents(id);

            Context.Batches contBatch = new Context.Batches();
            var days = contBatch.SelectDays(id);
            int totaldays = contBatch.CalculateDays(days);

            var duration = contBatch.GetallBatchList().Where(p => p.Id == id);
            var batchduration = (from c in duration where c.Id == id select c.Duration).ToList();
            string d = batchduration[0].ToString();

            int totalduration = contBatch.GetBatchDuration(d);

            int TotalCheckBoxes = totalduration * totaldays;

            List<string> getdays = contBatch.getdaysinList(days, totalduration);
            List<Models.Days> day = new List<Models.Days>();

            for (int i = 0; i < TotalCheckBoxes; i++)
            {
                day.Add(new Models.Days { dayid = i, dayname = getdays[i], ischecked = false });

            }

            viewmodel.days = day;

            return View(viewmodel);
        }

 [HttpPost]
        public ActionResult MarkAttendance(Models.StudentAttendance viewmodel)
        {
            Models.StudentAttendance viewmodel1 = new Models.StudentAttendance();
            //viewmodel.studentbatch = new Models.StudentBatch();
            //viewmodel.user = new Models.User();
            return View();
        }

AssignedStudents.cshtml

@model WebApplication1.Models.StudentAttendance

@{
    ViewBag.Title = "AssignedStudents";
}

<h2>AssignedStudents</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm("MarkAttendance","Instructor", FormMethod.Post))
{
<table class="table">
    <tr>

        <th>@Html.DisplayName("First Name")</th>
        <th>@Html.DisplayName("Last Name")</th>
    </tr>

@for (int j = 0; j < Model.userlist.Count; j++)
{

    <tr>
        <td>@Html.HiddenFor(m=>Model.userlist[j].Id)</td>
        <td>@Html.EditorFor(m => Model.userlist[j].FirstName)</td>
        <td>@Html.EditorFor(m => Model.userlist[j].LastName)</td>  
        @for (int i = 0; i < Model.days.Count; i++)
        {
            <td>
                @Html.CheckBoxFor(m => Model.days[i].ischecked)
                @Model.days[i].dayname
                @Html.HiddenFor(m => Model.days[i].dayid)
                @Html.HiddenFor(m => m.days[i].dayname)
            </td>
        } 
    </tr>  
 }
</table>

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" id="Attendance" value="Create" class="btn btn-default" />
    </div>
</div>


}

【问题讨论】:

  • 您刚刚创建了重复的复选框,具有重复的name 属性(以及重复的id 属性,这是无效的html)。 DefaultModelBinder 只会绑定第一个匹配的名称。目前尚不清楚您想要实现什么。每个User 是否应该有一个Days 的集合? (在这种情况下你的模型是错误的)
  • 是的,每个用户都有一组天数。
  • 那么您需要一个用户(学生?)的视图模型,其中包含 Days 的集合属性,然后使用嵌套的 for 循环
  • 这也无济于事。在列表中定义上课天数的属性也只会发布一行复选框值。
  • 那你没做对!

标签: checkbox asp.net-mvc-5


【解决方案1】:

解决了在控制器类中使用 viewmodel 作为 List 的问题。

StudentAttendance.cs

List<DayVM> days = new List<DayVM>
{
    new DayVM(),
    new DayVM()
};

List<StudentVM> model = new List<StudentVM>
{
    new StudentVM{ Id = 1, FirstName = "Bob", Days = days },
    new StudentVM{ Id = 2, FirstName = "john", Days = days },
}
return View(model);

内景

@for(int i = 0; i < Model.Count; i++)
{
    ... elements for properties of Student
    @for(int j = 0; j < Model[i].Days.Count; j++)
    {
        @Html.CheckBoxFor(m => m[i].Days[j].IsSelected)
        ....
    }
}

并采用 List 来收集帖子操作中的值

public ActionResult MarkAttendance(List<Models.StudentVM> lst)
{
.....
}

感谢 Stephen Muecke 让这一切变得简单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    相关资源
    最近更新 更多