Asp.net mvc 2 在ASP.NET MVC 2中使用ListBoxFor
我正在尝试更新特定组在我的应用程序中的角色。我在视图中使用的组模型附加了一个AllRoles IEnumerable,因此在我的视图中,我可以执行以下操作:Asp.net mvc 2 在ASP.NET MVC 2中使用ListBoxFor,asp.net-mvc-2,html-helper,Asp.net Mvc 2,Html Helper,我正在尝试更新特定组在我的应用程序中的角色。我在视图中使用的组模型附加了一个AllRoles IEnumerable,因此在我的视图中,我可以执行以下操作: <%: Html.ListBoxFor( model => model.aspnet_Roles, new MultiSelectList( Model.AllRoles, "RoleId", "RoleName" ), new { @class = "multiselect" } )%> [HttpPost] publ
<%: Html.ListBoxFor( model => model.aspnet_Roles, new MultiSelectList( Model.AllRoles, "RoleId", "RoleName" ), new { @class = "multiselect" } )%>
[HttpPost]
public ActionResult Edit(SYSGroups updatedGroup)
{
// updatedGroup.Aspnet_Roles will contain an array of all the RoleIds
// selected in the multiselect list.
return View();
}
谢谢所选ID将作为集合发送:
[HttpPost]
public ActionResult Edit(string[] aspnet_Roles)
{
// the aspnet_Roles array will contain the ids of the selected elements
return View();
}
如果表单包含需要发布的其他元素,则可以更新模型:
public class SYSGroups
{
public string[] Aspnet_Roles { get; set; }
... some other properties
}
让你的行动方法如下所示:
<%: Html.ListBoxFor( model => model.aspnet_Roles, new MultiSelectList( Model.AllRoles, "RoleId", "RoleName" ), new { @class = "multiselect" } )%>
[HttpPost]
public ActionResult Edit(SYSGroups updatedGroup)
{
// updatedGroup.Aspnet_Roles will contain an array of all the RoleIds
// selected in the multiselect list.
return View();
}
我可以避免在控制器功能中使用其他参数吗?我可以使用收到的aspnet_组来阅读收藏吗?public ActionResult Edit(SYSGroups updategroups){}是的,将
Aspnet_Roles
array属性添加到SYSGroups
model类中,如我所示。您在Html.ListBoxFor
中绑定到的属性需要是一个类似Html.ListBoxFor(model=>model.aspnet_角色,
-aspnet_角色
的数组。只需将该参数添加到控制器函数中就可以了,为我的model.Thx添加权重没有意义。