开发者

Using ListBoxFor in ASP.NET MVC 2

开发者 https://www.devze.com 2023-01-05 06:40 出处:网络
I am trying to update the Roles a specific group has in my application. The Group model I use in my view has an additional AllRoles IEnumerable attached to it, so that in my view I can do something li

I am trying to update the Roles a specific group has in my application. The Group model I use in my view has an additional AllRoles IEnumerable attached to it, so that in my view I can do something like this:

<%: Html.ListBoxFor( model => model.aspnet_Roles, new MultiSelectList( Model.AllRoles, "RoleId", "RoleName" ), new { @class = "multiselect" } )%>

This generates a multiple select drop down as expected. However, coming form PHP, I noticed that the name of the select was without square brackets, maybe that is OK in ASP.NET but in PHP it is wrong. Now, how do I go about updating the group after submiting the form, more precisely, how can I read the multiselct selected values. What I need is that based on the RoleIds that I receive to Add respective aspnet_Roles to my Group model.

Trying to read the received values using HttpContext.Request.Form["aspnet_Roles"] failed and is also ugly. Can I somehow use the model to fetch the needed data? Controller function:

[AcceptVerbs( HttpVerbs.P开发者_Python百科ost )]
public ActionResult Edit( SYSGroups updatedGroup ) {}

Thanks


The selected ids will be sent as a collection:

[HttpPost]
public ActionResult Edit(string[] aspnet_Roles) 
{
    // the aspnet_Roles array will contain the ids of the selected elements
    return View();
}

If the form contains other elements that need to be posted you could update your model:

public class SYSGroups
{
    public string[] Aspnet_Roles { get; set; }
    ... some other properties
}

and have your action method look like this:

[HttpPost]
public ActionResult Edit(SYSGroups updatedGroup) 
{
    // updatedGroup.Aspnet_Roles will contain an array of all the RoleIds
    // selected in the multiselect list.
    return View();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号