Asp.net mvc 2 Collections.Generic.Dictionary从Viewmodel到controller的模型绑定
我有一个问题: 当我试图将提交从视图发布到httppost actionResult时,我总是得到一个空值 这是我的代码:Asp.net mvc 2 Collections.Generic.Dictionary从Viewmodel到controller的模型绑定,asp.net-mvc-2,data-binding,model,asp.net-4.0,Asp.net Mvc 2,Data Binding,Model,Asp.net 4.0,我有一个问题: 当我试图将提交从视图发布到httppost actionResult时,我总是得到一个空值 这是我的代码: public class WhiteListViewModel { public string Badge { get; set; } public IEnumerable<string> Selezioni { get; set; } public IEnumerable<bool> Abilitazioni { g
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>
我哪里做错了?绑定过程将尝试将IEnumerable映射到HttpPost操作的参数白名单。但是,我相当肯定这是失败的,因为绑定过程没有信息将提交的字段与预期的参数白名单联系起来 您有几个选择: 尝试在您的操作中使用TryUpdateModel 创建自定义ModelBinder。这将允许您对提交的模型进行互操作,并在将IEnumerable传递给action参数之前构建IEnumerable 您可以在操作中使用FormCollection对象对提交的字段进行互操作。这有点凌乱,不理想 简化你的观点 就我个人而言,我会看看ModelBinder。如果有什么区别的话,这将让您更好地了解为什么模型可能没有绑定到动作参数