Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to index dynamically inserted partial view with asp-for tag helpers?

$
0
0

I've scoured the internet today for a solution to this, but have not come up with one.

I have a form with an "add" button that allows me to insert rows of a partial view to add more items to my form.  The partial view uses a strongly-typed model and contains a set of inputs using asp-for tag helpers.  Example:

Model:

public class Product
{
  public int ProductId {get;set;}
  public string Name {get;set;}
  public int Quantity {get;set;}
}

Partial View:

@model MyApp.Models.Product

<input type="hidden" asp-for="ProductId"><input type="text" asp-for="Name"><input type="text" asp-for="Quantity">

Post-Action:
[HttpPost]
public ActionResult CreateProducts(List<Product> products)

I can successfully add multiple rows of my partial view, however I'm unable to add indexing to the them so they are grouped properly in the post-action.  Now I have seen someone use indexing when using a ViewModel (example: asp-for="Product[0].Name"), however my partial view does not use a multi-class viewmodel.  It's just the "Product" class.  So what is the proper way to add indexing to asp-for when you only have a single class?

Things I have tried that didn't work:

asp-for="Model[0].Name"
asp-for="Model.[0].Name"
asp-for="[0].Name"
asp-for="Name[0]"


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>