【问题标题】:ASP.NET Core MVC view displaying incorrect values from input in listASP.NET Core MVC 视图显示列表中输入的错误值
【发布时间】:2017-07-16 14:00:11
【问题描述】:

我正在 ASP.NET Core MVC Web App 中构建报价单。我有一个 QuoteVM,其中包含报价所需的属性以及一个 List QuoteItems 以在报价上动态存储许多项目:

public class QuoteVM
{
    public string Status { get; set; }

    public QuoteItemVM NewQuoteItem { get; set; }

    #region QuoteProperties
    // Not relevant
    #endregion

    #region Company
    // Not relevant
    #endregion

    #region Client
    // Not relevant
    #endregion

    public List<QuoteItemVM> QuoteItems { get; set; }

    public List<string> WorkItems = new List<string>();

    public List<string> UnitList = new List<string>
    {
        "lm",
        "fm",
        "dag",
        "heild",
        "stk",
        "klst"
    };
}

在我看来,我添加了一个带有自动完成功能的输入字段,以便在选择报价项目时添加报价项目,我的表单将 VM 发布到我在控制器中的 POST 操作,并再次返回 QuoteVM,并将新的报价项目添加到QuoteItems 列表,该列表在将其发送到视图之前按报价项目编号属性排序。调试时,我可以看到列表已正确发送到视图,我还可以通过在段落中写出我的 QuoteItems 列表来验证。在这里,我在报价单中添加了 4 项,一切都按预期工作: Values are still correct

现在我们终于遇到了一个问题,即当我添加一个数字低于我在表格中的报价项目时,它在渲染表格时开始显示错误的值,但列表将是正确的,因为控制器是正确返回列表: Where things start to go south...

我的观点中相关的部分:

@{ // display values to check if they're correct
   foreach (var item in Model.QuoteItems)
   {
      <p>@item.Number - @item.Description - @item.Unit</p>
   }
}

<tbody>
@{  // the goal is to display them correctly here
   for (int i = 0; i < Model.QuoteItems.Count(); i++)
   {
   <tr>
      <td><input type="checkbox" class="i-checks" name="input[]"></td>
      <td><input asp-for="QuoteItems[i].Number" class="form-control text-center input-sm" /></td>
      <td><input asp-for="QuoteItems[i].Description" placeholder="Verkþáttur" class="form-control input-sm" /></td>
      <td><input asp-for="QuoteItems[i].Quantity" placeholder="Magn" class="form-control text-center input-sm jsQuantity calculate" /></td>
      <td><input asp-for="QuoteItems[i].Unit" placeholder="Eining" class="form-control text-center units input-sm" /></td>
      <td><input asp-for="QuoteItems[i].UnitPrice" placeholder="Einingarverð" class="form-control text-center input-sm jsUnitPrice calculate" /></td>
      <td><input asp-for="QuoteItems[i].TotalPrice" class="form-control text-center input-sm jsTotalPrice" /></td>
      <td><input asp-for="QuoteItems[i].TotalPriceVAT" class="form-control text-center input-sm jsTotalPriceVAT" /></td>
     </tr>
     }
}
</tbody>

有没有人遇到过类似的问题或知道是什么原因造成的?

【问题讨论】:

    标签: c# forms razor asp.net-core asp.net-core-mvc


    【解决方案1】:

    我的解决方案是在要查看的模型之前添加 ModelState.Clear()。

    【讨论】:

      猜你喜欢
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2020-09-25
      • 2019-10-15
      相关资源
      最近更新 更多