1

我对 MVC 有一些问题,我将尝试描述。我的模型中有 2 个班级。

public class ApplicationPermissionVM
    {
        public ApplicationPermission Permission { get; set; }
        public bool IsSelected { get; set; }
    }

public class RoleAndPermissonsModel
{
        //Constructor skipped

        public ApplicationRole ApplicationRole { get; set; }
        public IEnumerable<ApplicationPermissionVM> Permissions { get; set; }
}

第二个模型是主模型,我在控制器中初始化模型。ApplicationRole 为空,我在列表中有 19 个元素。当我发布表单时,将创建 ApplicationRole 成员,但权限列表将为空,因此将丢失所有选择。如果有人知道是什么问题,请写信给我。控制器:

[HttpGet]
public ActionResult NewRole()
{
    _model = new RoleAndPermissonsModel();
    return View(_model);
}

[HttpPost]
public ActionResult NewRole(RoleAndPermissonsModel newRole)
{
    if (ModelState.IsValid)
    {
        var id = _applicationRoleService.AddNewRole(newRole.ApplicationRole);
        _applicationRoleService.AssignPermissionsToRole(newRole.SelectedItems, id);
    }

    return RedirectToAction("Index");
}

看法:

@model EO.Core.WebUI.Models.RoleAndPermissonsModel
@using (Html.BeginForm("NewRole", "PermissionRole", FormMethod.Post, new { id = "frmNewRole" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>ApplicationRole</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.ApplicationRole.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ApplicationRole.Name)
            @Html.ValidationMessageFor(model => model.ApplicationRole.Name)
        </div>
        <table>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.IsSelected)
                </th>
                <th></th>
            </tr>

            @foreach (var item in Model.Permissions)
            {
                <tr>
                    <td>
                        @Html.EditorFor(modelItem => item.IsSelected)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Permission.Name);
                    </td>
                </tr>
            }
        </table>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
4

1 回答 1

1

您需要手动遍历列表并像这样输出

<input type="text" 
       id="Permissions[0].IsSelected" 
       name="Permissions[0].IsSelected" value="" />

因此,将您的代码更改为:

@for(int i = 0; i < Model.Permissions.Count; i++)
{
    <tr>
        <td><input type="checkbox" 
                   id="Permissions[@(i)].IsSelected" 
                   name="Permissions[@(i)].IsSelected" /></td>
        <td><input type="text" 
                   id="Permissions[@(i)].Permission.Name" 
                   name="Permissions[@(i)].Permission.Name" /></td>
    </tr>
}

因为您的类型中有一个对象,请Permissions记住初始化它,否则您将获得空引用。

public class RoleAndPermissonsModel
{
    public RoleAndPermissonsModel() {
        this.Permissions = new List<ApplicationPermissionVM>();
    }

    public ApplicationRole ApplicationRole { get; set; }
    public IEnumerable<ApplicationPermissionVM> Permissions { get; set; }
}

或者您可以轻松地创建一个ViewModel并使用它来将您的模型链接到您的视图。

注意:我确实想知道,如果您的列表始终为空(当您传递一个空/空列表时),您为什么要循环遍历Model.Permissions


为了执行“编辑”视图,循环代码应为:

@for(int i = 0; i < Model.Permissions.Count; i++)
{
    <tr>
        <td><input type="checkbox" 
                   @(Permissions[i].IsSelected ? "checked='checked'" : "")
                   id="Permissions[@(i)].IsSelected" 
                   name="Permissions[@(i)].IsSelected" /></td>
        <td><input type="text" 
                   id="Permissions[@(i)].Permission.Name" 
                   name="Permissions[@(i)].Permission.Name"
                   value="@(Permissions[i].Permission.Name)" /></td>
    </tr>
}

您需要添加的只是checked复选框(如果已选中)并将权限名称填充到其他输入。

于 2013-06-24T11:59:33.837 回答