ASP.NET MVC - 模型绑定一组动态生成的复选框 - 如何 [英] ASP.NET MVC - Model binding a set of dynamically generated checkboxes - how to

查看:122
本文介绍了ASP.NET MVC - 模型绑定一组动态生成的复选框 - 如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绑定模型一组动态生成的复选框,以便在控制器动作来处理它们,但不能得到模型绑定发生。这是该方案:

I'm trying to model bind a set of dynamically generated checkboxes so as to process them in the controller action but can't get the model binding to occur. This is the scenario:

我的ViewModel类(DocumentAddEditModel)包含一个字典(词典<字符串,布尔>)每个条目是为每个复选框的名称/标签和指示复选框是否被选中布尔串:

My ViewModel class (DocumentAddEditModel) contains a dictionary (Dictionary<string,bool>) with the string of each entry being the name/label for each checkbox and the boolean indicating whether the checkbox is checked:

    public class DocumentAddEditModel
    {
    	...
        private Dictionary<string, bool> _categoryCheckboxes = new Dictionary<string,bool>();
    	...

    	...
        public Dictionary<string, bool> CategoryCheckboxes
        {
            get { return _categoryCheckboxes; }
            set { _categoryCheckboxes = value; }
        }
    	...
    }
}

在我的控制器处理窗体的GET请求的动作填充dictonary如下:

Within my controller the action that handles the GET request for the form populates the dictonary as follows:

public class DocumentsController : Controller
{
    [RequiresAuthentication]
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Add()
    {
    	DocumentAddEditModel documentAddEditModel = new DocumentAddEditModel();
    	...
    	Dictionary<string, bool> categoryCheckboxes = new Dictionary<string, bool>();
    	...
    	string[] categories = Enum.GetNames(typeof(Category));

    	foreach (string category in categories)
    		categoryCheckboxes.Add(category, false);

    	documentAddEditModel.CategoryCheckboxes = categoryCheckboxes;

    	return View(documentAddEditModel);
    }
}

在视图里我有以下生成的复选框:

Within the view i have the following to generate the checkboxes:

<% foreach (KeyValuePair<string, bool> categoryCheckbox in ViewData.Model.CategoryCheckboxes)
    {%>
    <input class="checkbox" type="checkbox" name="CategoryCheckboxes[0].Key" id="<%= categoryCheckbox.Key %>" />
    <label class="categoryLabel" for="<%= categoryCheckbox.Key %>"><%= categoryCheckbox.Key %></label>
<% } %>

但我认为这是问题的必须。不知道需要在name属性走了。现在的问题是,一旦调回在DocumentsController以下操作方法:

but i think this is where the problem must be. Not sure what needs to be going in the name attribute. The problem is that once posted back to the following action method in the DocumentsController:

[RequiresAuthentication]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(DocumentAddEditModel documentAddEditModel)
{
    ...
}

documentAddEdit.Model.CategoryCheckboxes总是空。我该如何设置这个向上,以便在CategoryCheckboxes字典正确名称为填充并选中/取消布尔值的复选框?

documentAddEdit.Model.CategoryCheckboxes is always null. How do i set this up so that the CategoryCheckboxes dictionary is correctly populated with name and checked/unchecked bool value for the checkboxes?

感谢

推荐答案

如果你绑定你的复选框到词典&LT;字符串,布尔&GT; 试试这个:

If you are binding your checkboxes to a Dictionary<string, bool> try this:

<% var i = 0; %>
<% foreach (KeyValuePair<string, bool> categoryCheckbox in Model.CategoryCheckboxes) {%>

    <input type="hidden" name="<%= String.Format("CategoryCheckboxes[{0}].Key", i) %>" value="<%= categoryCheckbox.Key %>" />
    <%= Html.CheckBox(String.Format("CategoryCheckboxes[{0}].Value", i), categoryCheckbox.Value) %>

    <label class="categoryLabel" for="<%= categoryCheckbox.Key %>"><%= categoryCheckbox.Key %></label>

    <% i++; %>
<% } %>

希望这有助于

更新:

有关绑定到的IDictionary&LT; T1,T2&GT; 表单必须包含CategoryCheckboxes [N]。重点输入和CategoryCheckboxes [N] .value的IDS /名,其中n必须从零开始,从未间断。

For binding to IDictionary<T1, T2> your form must contain inputs with "CategoryCheckboxes[n].Key" and "CategoryCheckboxes[n].Value" Ids/Names, where n must be zero-based and unbroken.

这篇关于ASP.NET MVC - 模型绑定一组动态生成的复选框 - 如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆