如何使用@ Html.ListBoxFor与对象列表?

时间:2017-05-11 21:29:24

标签: c# asp.net asp.net-mvc-5

我是asp.net的新手,即使我在这里看到一些类似的问题,我也无法解决我的问题。

我有一个对象Permission列表(用id,名称,描述等组成..)我想使用这个列表创建一个带有@ Html.ListBoxFor的多重选择来获取包含选项的选项的是:

<option value="Id of the permission">Name of the permission</option>

这是我的控制器:

[ChildActionOnly]
        public PartialViewResult _GroupAdd(ManagerGroupViewModel groupe)
        {
            if (groupe != null)
            {
                //Here isn't important
                return PartialView(groupe);
            }
            else
            {
                ManagerGroupViewModel grp = new ManagerGroupViewModel();
                List<PermissionDTO> list = FactoryBO.Users.PermissionBO.GetAll();
                grp.permissionList = list;
                return PartialView("_GroupAdd", grp);
            }
        }

这里是我的部分观点:

@using (Ajax.BeginForm("_GroupAdd", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp" }))
{
<form class="col s12">
    <div class="row">
        <div class="input-field col s12">
            @Html.LabelFor(model => Model.groupe.Name, "Group name")
            @Html.EditorFor(model => Model.groupe.Name, new { htmlAttributes = new { @class = "validate" } })
            @Html.ValidationMessageFor(model => Model.groupe.Name, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="input-field col s12">
    </div>
    @Html.ListBoxFor(model => model.groupe.Permissions, new SelectList(Model.permissionList) )
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-primary" />
        </div>
    </div>
</form>
}

目前,我只是得到了这个:

<li class="">
<span><input type="checkbox"><label></label>{"Id":FHdqsbg,"Description":"Menu Principal. NOTA: Par defaut ","Name":"AccessMainMenu"}</span>
</li> 

如果有人可以帮助我:)。

1 个答案:

答案 0 :(得分:0)

DropDownListFor中添加Partial,就像那样:

<div class="input-field col s12">
    @Html.LabelFor(model => Model.groupe.Name, "Group name")
    @Html.EditorFor(model => Model.groupe.Name, new { htmlAttributes = new { @class = "validate" } })
    @Html.ValidationMessageFor(model => Model.groupe.Name, "", new { @class = "text-danger" })
    @Html.LabelFor(model => model.permissionList.UserRoleId)
    @Html.DropDownListFor(m => m.UserRoleId, new SelectList(model.permissionList, "RoleId", "RoleName"))
</div>

UserRoleIdRoleName更改为列表中字段的实际名称。

<强>参考文献: