绑定复选框值以在模型mvc中列出

时间:2012-12-07 15:08:43

标签: asp.net-mvc asp.net-mvc-3

我的问题是我必须创建如下布局使用MVC为用户分配权限。Sample of the lay out needed

现在创建复选框没有问题我将使用用户列表创建它。 但在提交表格时,我应该将其提交到下面的列表中。

public class UserRightsViewModel
    {
       public UserRightsViewModel()
        {
            _screenrights = new List<ScreenRight>();

        }
        public String Id { get; set; }// Role Name 
        List<ScreenRight> _screenrights;
        public List<ScreenRight> ScreenRights { get { return _screenrights; } set { _screenrights = value; } }

    }

screenRight的定义在

之下
public class ScreenRight
{
       public String UserName { get; set; }
        public Boolean Select{ get; set; }
        public Boolean Add{ get; set; }
        public Boolean Edit{ get; set; }
       ,,,
}

现在,在提交表单时,我如何以正确的格式将其发布到控制器。

2 个答案:

答案 0 :(得分:7)

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new UserRightsViewModel
        {
            // Obviously those could come from some data source
            ScreenRights = new[]
            {
                new ScreenRight { UserName = "Robert", Select = true, Add = false, Edit = false },
                new ScreenRight { UserName = "John", Select = true, Add = true, Edit = false },
                new ScreenRight { UserName = "Mike", Select = true, Add = true, Edit = false },
                new ScreenRight { UserName = "Allan", Select = true, Add = true, Edit = true },
                new ScreenRight { UserName = "Richard", Select = false, Add = false, Edit = false },
            }.ToList()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(UserRightsViewModel model)
    {
        // The view model will be correctly populated here
        // TODO: do some processing with them and redirect or
        // render the same view passing it the view model
        ...
    }
}

查看:

@model UserRightsViewModel

@using (Html.BeginForm())
{
    <table>    
        <thead>
            <tr>
                <th>User Id</th>
                <th>Select</th>
                <th>Add</th>
                <th>Edit</th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.ScreenRights.Count; i++)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(x => x.ScreenRights[i].UserName)
                        @Html.HiddenFor(x => x.ScreenRights[i].UserName)
                    </td>
                    <td>
                        @Html.CheckBoxFor(x => x.ScreenRights[i].Select)
                    </td>
                    <td>
                        @Html.CheckBoxFor(x => x.ScreenRights[i].Add)
                    </td>
                    <td>
                        @Html.CheckBoxFor(x => x.ScreenRights[i].Edit)
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <button type="submit">OK</button>
}

进一步阅读:Model Binding To a List

答案 1 :(得分:6)

要向后工作,最终的HTML应如下所示(忽略您的表格)

<input type="hidden" value="0" name="ScreenRights[0].Select"/>  
<input type="checkbox"  name="ScreenRights[0].Select"/>

<input type="hidden" value="0" name="ScreenRights[0].Add"/>  
<input type="checkbox" name="ScreenRights[0].Add"/>

<input type="hidden" value="0" name="ScreenRights[0].Edit"/> 
<input type="checkbox" name="ScreenRights[0].Edit"/>

<input type="hidden" value="0" name="ScreenRights[1].Select"/>  
<input type="checkbox"  name="ScreenRights[1].Select"/>

<input type="hidden" value="0" name="ScreenRights[1].Add"/>  
<input type="checkbox"  name="ScreenRights[1].Add"/>

<input type="hidden" value="0" name="ScreenRights[1].Edit"/> 
<input type="checkbox"  name="ScreenRights[1].Edit"/>

这个想法是索引顺序显示在属性名称的[i]数组部分中,然后链接到下一个属性。它应该以与你的i相同的顺序绑定。这里的另一个关键是复选框仅与CHECKED属性绑定,因此值对绑定器没有意义。这就是为什么你在前面有隐藏的输入。绑定程序将始终指定false,如果选中该复选框,则覆盖为true。您可以在简单模型上使用Html.CheckBoxFor验证此结果html。

在让HTML看起来像这样,你可以手动完成,或者你可以使用内置框架。

我很确定你可以在for循环中做这个(我作为迭代器)

@Html.CheckBoxFor(m => m.ScreenRights[i].Select)
@Html.CheckBoxFor(m => m.ScreenRights[i].Add)
@Html.CheckBoxFor(m => m.ScreenRights[i].Add)