简体   繁体   English

CheckBox在ASP.NET MVC中列出并将其绑定回控制器

[英]CheckBox lists in ASP.NET MVC and bind it back to controller

 Html.CheckBox("SelectedStudents", false, new { @class = "check-item", id = x.Id, value = x.Id })

which produce 哪个产生

<input checked="checked" class="check-item" id="4507" name="SelectedStudents" value="4507" type="checkbox">

<input checked="checked" class="check-item" id="4507" name="SelectedStudents" value="4508" type="checkbox">

<input checked="checked" class="check-item" id="4507" name="SelectedStudents" value="4509" type="checkbox">

In mvc model I have 在mvc模型中,我有

public IEnumerable<string> SelectedStudents { get; set; }

but when I post back, SelectedStudents are always null. 但是当我回帖时,SelectedStudents总是为空。 Why? 为什么? In this howto http://benfoster.io/blog/checkbox-lists-in-aspnet-mvc is written: 在这个方法http://benfoster.io/blog/checkbox-lists-in-aspnet-mvc写的:

The ASP.NET MVC modelbinder is smart enough to map the selected items to this property. ASP.NET MVC模型绑定器非常智能,可以将所选项映射到此属性。

but in my example is always null. 但在我的例子中总是为空。 Why? 为什么? How to write more checkboxes and bind it back 如何编写更多复选框并将其绑定回来

You should be using a strongly typed editor to be able to pass the result to the controller (Model binder). 您应该使用强类型编辑器将结果传递给控制器​​(模型绑定器)。

I prefer to do it this way. 我更喜欢这样做。

Model 模型

public class YourViewModel
{
     public List<SelectListItem> Students
        {
            get;
            set;
        }
}

Controller Get 控制器获取

Students= service.GetStudents(); //Fill the list

View 视图

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

                    @Html.CheckBoxFor(m => m.Students[i].Selected)
                    @Html.HiddenFor(m => m.Students[i].Text)
                    @Html.HiddenFor(m => m.Students[i].Value)
                    <span>@Model.Students[i].Text</span>
                }

Controller Post 控制器帖子

[HttpPost]
        public ActionResult Create(YourViewModel model)
        {
          foreach(var student in model.Students)
          {
            if(student.Selected) { // Do your logic}
          }
        }

Alternatively You could use an array or List of string. 或者,您可以使用数组或字符串列表。 A ListBox is used in this example. 在此示例中使用ListBox。

public string[] SelectedStudents{ get; set; }

@Html.ListBoxFor(s => s.SelectedStudents, new MultiSelectList(Model.Students, "Value", "Text", Model.SelectedStudents), new { @class = "form-control", style = "height:250px; width:100%" })

See my answer here How to bind checkbox values to a list of ints? 在此处查看我的答案如何将复选框值绑定到整数列表? .

The nice thing about this is that it separates concerns between your controller and ui nicely. 关于这一点的好处是它可以很好地区分控制器和ui之间的关注点。 The html extension methods also create correct html using label and input for the checkbox. html扩展方法还使用标签和输入为复选框创建正确的html。 and there is no need for hidden fields. 并且不需要隐藏的字段。

Do you try it with CheckBoxListFor?? 你用CheckBoxListFor尝试吗? You need to associate the checkbox with model and should not have the same ID and name 您需要将复选框与模型关联,并且不应具有相同的ID和名称

@Html.CheckBoxListFor(model => model.SelectedSources, Model.SubscriptionSources)

您需要使用可变类型,例如List<string>

public List<string> SelectedStudents { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM