2011-02-09 9 views
0

私はこの問題を抱えています: Viewからhttppost actionResultへの投稿を投稿しようとすると、常にnull値を取得します。Collections.Generic.Dictionary <TKey,Tvalue> Viewmodelからコントローラへのモデルバインド

これは私のコードです:

私が間違っているの
public class WhiteListViewModel 
{ 
    public string Badge { get; set; } 
    public IEnumerable<string> Selezioni { get; set; } 
    public IEnumerable<bool> Abilitazioni { get; set; } 
} 


public ActionResult WhiteList() 
{ 

    return View("Whitelist", MasterPage, new WhitelistViewModel()); 
} 

[HttpPost] 
public ActionResult WhiteListp(IEnumerable<WhiteListViewModel> Whitelist) 
{ 
      bool[] abilitato = new bool[Whitelist.Single().Abilitazioni.Count()]; 
      string[] selezione = new string[Whitelist.Single().Selezioni.Count()];    
... 
} 



    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/SiteR.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<_21112010.ViewModel.WhiteListViewModel>>" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    WhiteList 
</asp:Content 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <h2>WhiteList</h2>   
    <table style="width:100%;"> 
    <thead>  
</thead> 
    <tbody >    
     <%using (Html.BeginForm()) 
    {%> 
      <% foreach (var item in Model){%> 
        <tr style="width:100%;"> 
       <td > 
       <%: item.Badge%>     
       </td>     
       <%foreach (var abit in item.Abilitazioni){%> 
       <td >      
        <%: Html.CheckBoxFor(c=>abit/*, new { onchange = "this.form.submit();" } */)%> 
        <%: Html.ValidationMessageFor(c => abit) %> 
       </td>      
       <% } %> 
       <%} %> 
       <td style=" width:1px;" > 
       <%: Html.HiddenFor(model=>item.Badge) %> 
       <% foreach (var sel in item.Selezioni) {%> 
       <%: Html.HiddenFor(c=>sel) %> 
       <%} %> 
       </td> 
       </tr>         <%}%>        
     </tbody> 
     <tfoot> 
     </tfoot > 
     </table> 
    <input type="submit" value="Salva ed Esci" style = "background-color:Gray; color:#F6855E; font-weight:bold; border:1px solid black; height:20px;" /> 
     <%:Html.ActionLink("Aggiungi Badge", "AggiungiBadge")%>    
     <% } %>      
     </div> 
</asp:Content> 

答えて

0

バインディングプロセスは、IEnumerableをHttpPostアクションのホワイトリストパラメータにマップしようとします。しかし、私は、バインディングプロセスが、提出されたフィールドを期待されるパラメータ "ホワイトリスト"に結びつけるための情報を持っていないため、これが失敗していると確信しています。

あなたは、いくつかのオプションがあります。

  1. あなたのアクションでTryUpdateModel()を使用してみてください。

  2. カスタムModelBinderを作成します。これにより、送信されたモデルを相互作用させ、アクションパラメータに渡す前にIEnumerableを構築することができます。

  3. アクション内のFormCollectionオブジェクトを使用して、サブミットされたフィールドを相互参照することができます。これは少し面倒で理想的ではありません。

  4. ビューを簡略化します。

個人的には、ModelBinderを見ていきます。何かがある場合、モデルがアクションパラメータにバインドされない理由についてのより良い洞察が得られます。

関連する問題