2011-10-25 40 views
1

我有一個時刻。我似乎無法將ListBox中的選定項目綁定到處理post事件的操作方法的參數中。排除列表框中的所選項目的故障

ModelSystemRoleList類型的:該代碼生成ListBox

public class SystemRole { 
    public Int32 Id { get; set; } 

    public String Name { get; set; } 
} 

<%: this.Html.ListBox("Roles", new SelectList(Model.List, "Id", "Name")) %> 

動作方法接收所選擇的項目是

public class SystemRoleList { 
    public IEnumerable<SystemRole> List { get; set; } 
} 

SystemRole被定義爲像t一樣成立他:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult UpdateSystemRoles(Int32[] roles) { 
    // do something with the results.... 
} 

麻煩的是,roles總是null。我試過使用其他數據類型 - string[],ICollection<int>,等等。如果我做Request.Form["Roles[]"],我可以看到Request.Form集合中的值。如果我從ListBox中選擇了那些項目,典型值可能是1,3,4

如何命名ListBox或我的參數,以便MVC自動綁定這些值?

回答

1

奇怪的是,下面的作品完全適合我。

型號:

public class SystemRoleList 
{ 
    public IEnumerable<SystemRole> List { get; set; } 
} 

public class SystemRole 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

控制器:

[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"); 
    } 
} 

查看:

<%@ 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> 

這是說我會用ListBox助手的強類型版本,像這樣:

型號:

public class SystemRoleList 
{ 
    public int[] Roles { get; set; } 
    public IEnumerable<SystemRole> List { get; set; } 
} 

控制器:

[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"); 
    } 
} 

查看:

<%@ 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> 
+0

什麼是你的'Request.Form'收集什麼樣子的?你是否有'角色'的多個條目,或者他們是'角色[]'而不是? – Yuck

+0

@Yuck,'Request.Form [「Roles」] =「1,2」'和Request.Form [「Roles []」] = null'。我有多個角色的條目:「角色= 1和角色= 3」。 –

+0

所以我認爲這是問題 - 我的表單值包括角色[]而不是角色。這是在部分視圖中使用,所以更高的東西可能會干擾,並導致它發佈的方式。去猜測這是Telerik的MVC擴展。謝謝你的幫助。 – Yuck