I am working on a project that focuses on collecting data from a Razor page to save to a database. The challenge is the following: 1. I am building a Page that Users can use to Select 2. I do not know how many Items that Users will Select ahead of time 3. I am building a Drop-Down with Razor from a .NET Core Web App and JQuery to populate remaining info about the selection in a Table Row. 4. I would like to be able to parse through the Users Selection and save each Record to the database, under the User's Name and other personal information 5. I am done with getting both the Users list and Product Lists to the Razor page.
What I need assistance with is the Dynamic Block of Fields that need to be generated on demand or by Tab action to provide a new row that users can use to add a new product.
Here is the code that I am using now on the Razor page to collect the first row:
----------- Code Starts Here -------------------------------
<div style="width:100%;">
<table>
<th>Product Code</th>
<th>Product Description</th>
<th>Quantity</th>
<th>Product Price</th>
<tr>
<td>
<select class="form-control" id="ProductCode" name="ProductCode">
<option selected>Select Product</option>
@foreach (var ulsp in ViewData["prod"] as List<Scafolding.UlslineItemCodes>)
{
<option value="@ulsp.Id" data-description="@ulsp.DefaultDescription"
data-price="@String.Format("{0:0.00}",ulsp.DefaultUnitPrice)">@ulsp.LineItemCode</option>
}
</select>
</td>
<td>
<input value="" class="form-control" id="ProductDesc" name="ProductDesc" style="width:500px;"/>
</td>
<td>
<input value="0" class="form-control" id="quantity" name="quantity" style="width:50px;" placeholder="0"/>
</td>
<td>
<input value="" class="form-control" id="ProductPrice" name="ProductPrice" style="width:95px;" placeholder="$0.00" />
</td>
</tr>
</table>
</div>
<script>
$(document).ready(function () {
// wire up the change handler http://api.jquery.com/change/
$("#ProductCode").change(function () {
var data = "";
// get the selected option's data values http://api.jquery.com/data/
data = $("#ProductCode option:selected").data();
// set the inputs
$("#ProductDesc").val(data.description);
$("#quantity").val(data.quant);
$("#ProductPrice").val(data.price).toFixed(2);
});
});
$(document).ready(function () {
$("#quantity").on("change", function () {
var quant = "";
var data = "";
data = $("#ProductCode option:selected").data();
quant = $("#quantity").val();
$("#ProductPrice").val(data.price * quant).toFixed(2);
});
});
</script>
--------------- Code Ends Here ---------------------
How do I dynamically generate a new block of this code to allow users to select a new Item?
What is the best way to parse through the data in C#, once it is submitted, and save it to a database for processing?
Thank you for your guidance and recommendations.