I'm having a moment. I can't seem to get the selected items from a ListBox
to be bound in a parameter to the action method that handles the post
event.
Model
is of type SystemRoleList
:
public class SystemRoleList {
public IEnumerable<SystemRole> List { get; set; }
}
SystemRole
is defined as:
public class SystemRole {
public Int32 Id { get; set; }
public String Name { get; set; }
}
This code generates the ListBox
:
<%: this.Html.ListBox("Roles", new SelectList(Model.List, "Id", "Name")) %>
The action method receiving the selected items is set up like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateSystemRoles(Int32[] roles) {
// do something with the results....
}
The trouble is that roles
is always null
. I've tried using other data types - string[]
, ICollection<int>
, and so on. I can see the values in the Request.Form
collection if I do Request.Form["Roles[]"]
. Typical values might be 1,3,4
if I selected those items from the ListBox
.
How can I name either the ListBox
or my parameter so that MVC will automatically bind the values?
Weird, the following works perfectly fine for me.
Model:
public class SystemRoleList
{
public IEnumerable<SystemRole> List { get; set; }
}
public class SystemRole
{
public int Id { get; set; }
public string Name { get; set; }
}
Controller:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new SystemRoleList
{
List = new[]
{
new SystemRole { Id = 1, Name = "role 1" },
new SystemRole { Id = 2, Name = "role 2" },
new SystemRole { Id = 3, Name = "role 3" },
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(int[] roles)
{
return Content("thanks for submitting");
}
}
View:
<%@ Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Controllers.SystemRoleList>"
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) { %>
<%= Html.ListBox("Roles", new SelectList(Model.List, "Id", "Name")) %>
<button type="submit">OK</button>
<% } %>
</asp:Content>
This being said I would use the strongly typed version of the ListBox
helper, like this:
Model:
public class SystemRoleList
{
public int[] Roles { get; set; }
public IEnumerable<SystemRole> List { get; set; }
}
Controller:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new SystemRoleList
{
List = new[]
{
new SystemRole { Id = 1, Name = "role 1" },
new SystemRole { Id = 2, Name = "role 2" },
new SystemRole { Id = 3, Name = "role 3" },
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(SystemRoleList model)
{
return Content("thanks for submitting");
}
}
View:
<%@ Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Controllers.SystemRoleList>"
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) { %>
<%= Html.ListBoxFor(x => x., new SelectList(Model.List, "Id", "Name")) %>
<button type="submit">OK</button>
<% } %>
</asp:Content>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.