简体   繁体   English

ASP.NET Core MVC视图显示列表中输入的错误值

[英]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.

相关问题 ASP.NET MVC,从视图获取模型列表值 - asp.NET mvc, get model list values from view 在 ASP.NET MVC 视图中显示来自 .NET 核心 API 的图像是否正确? - Is it correct way of displaying Image from .NET Core API in ASP.NET MVC View? 从表单输入(使用标签助手)获取日期并在 asp.net 核心 mvc 中显示该周的事件 - Getting a date from form input (with tag helpers) and displaying events in that week in asp.net core mvc 在asp.net core 2 MVC中通过ajax在视图中显示来自控制器的json字符串 - displaying json strings from controller in view over ajax in asp.net core 2 MVC ASP.Net MVC Dropdownlist List selected item values not displayed in the same View - ASP.Net MVC Dropdownlist List selected item values not displaying in the same View 复选框列表值未绑定到ASP.NET Core MVC中的View GET Request - Checkbox list values are not binding on View GET Request in ASP.NET Core MVC ASP.NET Core MVC Xreturn 查看多个值 - ASP.NET Core MVC Xreturn view multiple values 在Asp.net/Mvc Razor视图上显示从SQL Server数据库中检索的带有千位分隔符的十进制值 - Displaying decimal values retrieved from Sql Server Database with thousands seperator on an Asp.net/Mvc Razor view 模型的ASP.NET Core MVC List属性在视图中为空 - ASP.NET Core MVC List property of model empty in view 未将数据列表返回到 ASP.NET Core MVC 中的索引视图中 - Not returning a list of data into index view in ASP.NET Core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM