[英]Trouble Binding Selected Items from ListBox
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. 我似乎无法从
ListBox
获取所选项目,以将其绑定到处理post
事件的action方法的参数中。
Model
is of type SystemRoleList
: Model
的类型为SystemRoleList
:
public class SystemRoleList {
public IEnumerable<SystemRole> List { get; set; }
}
SystemRole
is defined as: SystemRole
定义为:
public class SystemRole {
public Int32 Id { get; set; }
public String Name { get; set; }
}
This code generates the ListBox
: 这段代码生成
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
. 问题在于
roles
始终为null
。 I've tried using other data types - string[]
, ICollection<int>
, and so on. 我试过使用其他数据类型
ICollection<int>
string[]
, ICollection<int>
等。 I can see the values in the Request.Form
collection if I do Request.Form["Roles[]"]
. 如果执行
Request.Form["Roles[]"]
则可以在Request.Form
集合中看到这些值。 Typical values might be 1,3,4
if I selected those items from the ListBox
. 如果我从
ListBox
选择了这些项目,则典型值为1,3,4
。
How can I name either the ListBox
or my parameter so that MVC will automatically bind the values? 如何命名
ListBox
或参数,以便MVC自动绑定值?
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: 据说我将使用
ListBox
帮助器的强类型版本,如下所示:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.