0

I'm pretty new with asp.net and even if I see some similar question here, I couldn't solve my problem.

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:

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

Nathan Meunier
  • 131
  • 1
  • 9

1 Answers1

0

Add a DropDownListFor in you Partial, like that:

<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 andRoleName to the actual names of your fields in the list.

References:

Community
  • 1
  • 1
George Wurthmann
  • 411
  • 2
  • 8
  • 20
  • A DropdownListFor is use to create a simple select. Not a multiple select,in my case here I need a multiple select. Know you how I can do that ? – Nathan Meunier May 12 '17 at 14:43
  • @NathanMeunier, why not to build your list in the `Controller` and then in your view you can just do like i did? In the answer If you want to do more filter in the view, you will expose your context, and it's not a good idea. If you have any doubt how to do that, open another question about you problem. – George Wurthmann May 12 '17 at 22:02