简体   繁体   English

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

[英]How works @Html.ListBoxFor with a list of object?

I'm pretty new with asp.net and even if I see some similar question here, I couldn't solve my problem. 我对asp.net相当陌生,即使我在这里看到类似的问题,也无法解决我的问题。

I have a list of an object Permission ( compose with an id, a name, a description etc.. ) and I want to use this list to create a multiple select with @Html.ListBoxFor to obtain a select containing option like that: 我有一个对象Permission的列表(由ID,名称,描述等组成),我想使用此列表通过@ Html.ListBoxFor创建多选,以获取一个包含以下内容的选择:

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

Here is my controller: 这是我的控制器:

[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);
            }
        }

here my partial view: 这是我的部分观点:

@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>
}

For the moment, I just obtain this : 目前,我只是得到这个:

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

if someone can help me :) 如果有人可以帮助我:)

Add a DropDownListFor in you Partial , like that: 在您的Partial添加一个DropDownListFor ,就像这样:

<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>

Change UserRoleId and RoleName to the actual names of your fields in the list. UserRoleIdRoleName更改为列表中字段的实际名称。

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM