【问题标题】:Model is always null when submitting form from Razor view从 Razor 视图提交表单时,模型始终为空
【发布时间】:2013-12-02 11:32:39
【问题描述】:

我正在开发一个ASP.NET MVC 4 应用程序。我的控制器中有一个名为 Currencty.cshtml 的视图和两个方法:

    [HttpGet]
    public ActionResult Currency()
    {
        IEnumerable<Currency> model = unitOfWork.CurrencyRepository.GetAll();
        return View(model);
    }

    [HttpPost]
    public ActionResult Currency(IEnumerable<Currency> model)
    {
        var test = model;

视图本身是:

@model IEnumerable<MyProject.Models.Currency>
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
<table>
   <thead>
     <tr>
         <th rowspan="2">Code</th>
         <th rowspan="2">Currency</th>
         <th rowspan="2">Fixing</th>
         <th colspan="2">PayDesk</th>
         <th colspan="2">NoDesk</th>
     </tr>
     <tr>
         <th>Buy</th>
         <th>Sell</th>
         <th>Buy</th>
         <th>Sell</th>
     </tr>
   </thead>
   <tbody>
     @foreach (var item in Model)
     { 
        <tr>
            <td>@Html.HiddenFor(modelItem => item.CurrencyID)
                @Html.TextBoxFor(modelItem => item.Code)</td>
            <td>@Html.TextBoxFor(modelItem => item.CurrencyName)</td>
            <td>@Html.TextBoxFor(modelItem => item.FixingRate)</td>
            <td>@Html.TextBoxFor(modelItem => item.PayDeskBuy)</td>
            <td>@Html.TextBoxFor(modelItem => item.PayDeskSell)</td>
            <td>@Html.TextBoxFor(modelItem => item.NoDeskBuy)</td>
            <td>@Html.TextBoxFor(modelItem => item.NoDeskSell)</td>
        </tr>
     }
    </tbody>

</table>
    <input type="submit" id="form-submit-button" value="Save" />
}

问题是我在[HttpPost] public ActionResult Currency(IEnumerable&lt;Currency&gt; model) 有一个断点,我可以看到当我提交表单时我到达那里,但模型始终为空。一切看起来都那么简单,我只是看不出出了什么问题,所以我无法取回我的数据。当我使用 [HttpGet] 方法加载表单时,一切正常,我看到了数据。

【问题讨论】:

    标签: c# asp.net-mvc-4 razor


    【解决方案1】:

    尝试使用for循环而不是foreach进行绑定

    @for(int i=0;i<Model.Count;i++)
        {
            <td>@Html.HiddenFor(modelItem => Model[i].CurrencyID)
                    @Html.TextBoxFor(modelItem => Model[i].Code)</td>
                <td>@Html.TextBoxFor(modelItem => Model[i].CurrencyName)</td>
                <td>@Html.TextBoxFor(modelItem => Model[i].FixingRate)</td>
                <td>@Html.TextBoxFor(modelItem => Model[i].PayDeskBuy)</td>
                <td>@Html.TextBoxFor(modelItem => Model[i].PayDeskSell)</td>
                <td>@Html.TextBoxFor(modelItem => Model[i].NoDeskBuy)</td>
                <td>@Html.TextBoxFor(modelItem => Model[i].NoDeskSell)</td>
        }
    

    Reference

    【讨论】:

    • 这似乎是一件微不足道的事情,但很难理解这两个循环的差异来自哪里。 +1 供参考,这实际上揭示了为什么 for 循环有效,而不仅仅是“这个有效”或“试试这个”的答案。
    • 无法将 [] 索引应用于“System.Collections.Generic.IEnumerable”类型的表达式有什么想法吗?
    • 我无法使用IEnumerable。我不知道为什么 - 只是更改为 List 也没有用我必须同时使用 List@for() 才能让它工作。在您当前的示例中,在i&lt;Model.Count 中使用&lt; 时出现错误。我错误地关闭了带有原始错误的页面,但它抱怨使用带有 int 和嵌套的 &lt;... 不知道
    【解决方案2】:

    为了能够列出对象列表的所有值并能够将所有这些值提交给 POST 操作,我遇到了一个奇怪的问题,即我总是将 null 提交给 POST 操作。所以,我将剃刀视图页面顶部的IEnumerable&lt;&gt; 列表更改为List&lt;&gt;,我没有使用@foreach(),而是使用了@for() 索引

    @for (int i = 0; i < Model.Count(); i++)
        {
        <tr>
            <td>@Html.DisplayFor(modelItem => Model[i].UserId)</td>
            @Html.HiddenFor(modelItem => Model[i].UserId)
            <td>@Html.CheckBoxFor(modelItem => Model[i].isLead)</td>
        </tr>
        }
    

    【讨论】:

      【解决方案3】:

      尝试在模型上使用for 循环和indexers

      @for(int i=0;i<Model.Count();i++)
          {
              <td>@Html.HiddenFor(modelItem => item.CurrencyID)
                      @Html.TextBoxFor(modelItem => Model[i].Code)</td>
                  <td>@Html.TextBoxFor(modelItem => Model[i].CurrencyName)</td>
                  <td>@Html.TextBoxFor(modelItem => Model[i].FixingRate)</td>
                  <td>@Html.TextBoxFor(modelItem => Model[i].PayDeskBuy)</td>
                  <td>@Html.TextBoxFor(modelItem => Model[i].PayDeskSell)</td>
                  <td>@Html.TextBoxFor(modelItem => Model[i].NoDeskBuy)</td>
                  <td>@Html.TextBoxFor(modelItem => Model[i].NoDeskSell)</td>
          }
      

      此外,为了应用索引器,您应该将 List 作为模型传递。所以在你的行动中:

      [HttpGet]
      public ActionResult Currency()
      {
          var model = unitOfWork.CurrencyRepository.GetAll().ToList();
          return View(model);
      }
      

      在你看来:

      @model List<MyProject.Models.Currency>
      

      如果您想了解有关列表绑定的更多详细信息,我建议您阅读以下文章:

      Model Binding To A List by Phill Haacked

      ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries by S. Hanselman

      【讨论】:

      • 无法将 [] 索引应用于“System.Collections.Generic.IEnumerable”类型的表达式有什么想法吗?
      • @Leron,你可以随时设置Model.ToList()
      • 尝试在Http Get方法中返回View(model.toList())
      • 是的,你是对的。在将其更改为 List 之前无法正常工作。你的例子没问题。
      猜你喜欢
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 2022-10-13
      • 1970-01-01
      • 2015-12-03
      相关资源
      最近更新 更多