2011-10-25 15 views
1

私は瞬間があります。私はListBoxから選択されたアイテムを、postイベントを処理するアクションメソッドのパラメータにバインドすることはできません。問題をListBoxから選択した項目にバインドする

ModelSystemRoleListである:このコードはListBoxを生成

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

    public String Name { get; set; } 
} 

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

SystemRoleは以下のように定義される

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

選択したアイテムを受信するアクションメソッドであります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またはmyパラメータのいずれかを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"); 
    } 
} 

ビュー:これは私がこのようなListBoxヘルパーの強く型付けされたバージョンを、使用する言われて

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

モデル:

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'コレクションはどのようなものが見えますか? 'roles'には複数のエントリがありますか、代わりに' roles [] 'ですか? – Yuck

+0

@Yuck、 'Request.Form [" Roles "] =" 1,2 "'と 'Request.Form [" Roles [] "] = null'です。ロールには複数のエントリがあります: 'Roles = 1&Roles = 3'。 –

+0

私はそれが問題だと思う - 私のフォームの値には 'roles'があり、' roles'は含まれていません。これは部分的なビューで使用されているので、より高いものがおそらく干渉し、その代わりにその方法で投稿する原因になります。この時点で、TelerikのMVC拡張機能を推測することになります。ご協力いただきありがとうございます。 – Yuck

関連する問題