[英]ASP.NET Core MVC view displaying incorrect values from input in list
I'm building a quotation form in ASP.NET Core MVC Web App. 我正在ASP.NET Core MVC Web App中建立报价单。 I've got a QuoteVM that contains required properties for the quote plus a List QuoteItems to store dynamically many items on a quote: 我有一个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"
};
}
In my view I've added an input field with autocomplete to add quote items when a quote item has been selected, my form posts the VM to my POST action in controller and returns the QuoteVM again with the new quote item added to the QuoteItems list, the list is ordered by quote item number property before sending it to the view. 在我的视图中,我添加了一个具有自动完成功能的输入字段,以在选择了报价项后添加报价项,我的表单将虚拟机发布到控制器中的POST操作中,并再次返回QuoteVM并将新的报价项添加到QuoteItems列表中,该列表将按报价项目编号属性排序,然后将其发送到视图。 When debugging I can see that the list is being sent correctly to the view which I can also verify by writing out my QuoteItems list in a paragraph. 调试时,我可以看到列表已正确发送到视图,也可以通过在段落中写出QuoteItems列表来验证该视图。 Here I've added 4 items to my quote and everything is working as expected: Values are still correct 在这里,我在报价中添加了4个项目,并且一切都按预期进行: 值仍然正确
Now we finally come to the problem which is that when I add an quote item that has a number lower than I have in my table it starts showing wrong values when rendering the table, but the list will be correct since the controller is returning the list correctly: Where things start to go south... 现在我们终于要解决的问题是,当我添加一个报价单,该报价单的编号比我的表中的编号低时,在呈现该表格时它开始显示错误的值,但是该列表将是正确的,因为控制器正在返回该列表正确: 事情开始往南走...
The parts of my view that are related: 我认为相关的部分:
@{ // 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>
Has anyone run into similar problems or knows what might be causing this? 是否有人遇到类似问题或知道是什么原因引起的?
我的解决方案是在查看模型之前添加ModelState.Clear()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.