简体   繁体   中英

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. I've got a QuoteVM that contains required properties for the quote plus a List QuoteItems to store dynamically many items on a quote:

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. 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. Here I've added 4 items to my quote and everything is working as expected: Values are still correct

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()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM