4

我的问题是我必须创建如下布局,使用 MVC 为用户分配权限。所需布局样本

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

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

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

4

2 回答 2

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

于 2012-12-07T15:35:43.350 回答
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 属性绑定,因此值对于绑定器而言没有任何意义。这就是为什么你在前面有隐藏的输入。binder 将始终分配 false,如果选中该复选框,则将其覆盖为 true。您可以在简单模型上使用 Html.CheckBoxFor 验证生成的 html。

就让您的 HTML 看起来像这样而言,您可以手动完成,也可以使用内置框架。

我很确定您可以在 for 循环中执行此操作(i 作为迭代器)

@Html.CheckBoxFor(m => m.ScreenRights[i].Select)
@Html.CheckBoxFor(m => m.ScreenRights[i].Add)
@Html.CheckBoxFor(m => m.ScreenRights[i].Add)
于 2012-12-07T15:27:16.377 回答