Collections.Generic.Dictionary从 Viewmodel 到控制器的模型绑定

发布于 2024-10-16 07:56:48 字数 2784 浏览 3 评论 0原文

我有这个问题: 当我尝试将提交从视图发布到 httppost actionResult 时,我总是得到一个空值。

这是我的代码:

    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>

我哪里做错了?

i have this problem :
when i try to post submit from a View to a httppost actionResult i get always a null value.

this is my code :

    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>

where am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2024-10-23 07:56:48

绑定过程将尝试将 IEnumerable 映射到 HttpPost 操作的参数 Whitelist。但是,我相当确定这是失败的,因为绑定过程没有信息将提交的字段绑定到预期的参数“白名单”。

您有几个选择:

  1. 尝试在您的操作中使用 TryUpdateModel();

  2. 创建自定义 ModelBinder。这将允许您询问提交的模型并在将其传递给操作参数之前构建您的 IEnumerable;

  3. 您可以在操作中使用 FormCollection 对象查询提交的字段。这有点混乱而且不理想;

  4. 简化您的视图。

就我个人而言,我会关注 ModelBinder。如果有的话,这将使您更好地了解模型可能未绑定到操作参数的原因。

The binding process will attempt to map the IEnumerable to the parameter Whitelist of the HttpPost action. However, I'm fairly sure that this is failing because the binding process has no information to tie the submitted fields to the expected parameter "Whitelist".

You have a few options:

  1. Try using TryUpdateModel() in your action;

  2. Creating a custom ModelBinder. This will allow you to interogate the submitted Model and build your IEnumerable prior to it being passed to the action parameter;

  3. You could interogate the submitted fields using the FormCollection object from within the action. This is a little messy and not ideal;

  4. Simplify your view.

Personally, I would look at the ModelBinder. If anything, this would give you a better insight into why the Model may not be binding to the action parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文