简体   繁体   English

无法绑定列表框中的选定项目

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM