[RESOLVED]how to create unique id's within repeating partial cshtmls?

Hello, I’m working on a website that displays different products for an outfit. I’ve created a partial cshtml for a Quantity input and this is repeated for each product for an outfit. I need to ensure that each Quantity input has a unique ID. What is the
most elegant way to do that through the MVC framework?

Are you just using a simple <input type="number"> to allow users to enter quantity for specific product?

AngelOfDeath

Are you just using a simple <input type="number"> to allow users to enter quantity for specific product?

yes

Using markup ID= or name= is the simplest and most direct way to do it.  And by that I guess also the most elegant.

So something like this:

<input type="text" ID="Quantity1_Product1" name="Quantity1_Product1">
<input type="text" ID="Quantity2_Product2" name="Quantity2_Product2">
<input type="text" ID="Quantity2_Product2" name="Quantity3_Product3">
<input type="text" ID="Quantity2_Product2" name="Quantity4_Product4">

When retrieving data, you can request each unique ID:

//to store / retrieve values 
var Quantity1_Product1 = Request.Form["Qunatity1_Product1"];
var Quantity2_Product2 = Request.Form["Qunatity2_Product2"];
var Quantity3_Product3 = Request.Form["Qunatity3_Product3"];
var Quantity4_Product4 = Request.Form["Qunatity4_Product4"];

//once values are stored you can do pretty much anything with them, insert / update database etc. 

You can start retrieving data in vars and insert them into your database directly in the .cshtml (view) page:

Just use @{

}

at the top.

Or you can do all of the data collection separately in a controller in MVC. So something like: ProductQuantityData.cs. 

Leave a Reply