This is my first question here at stackoverflow. Hehehe.
I have an edit page which I need to display all the existing fields and I have an add button which I can add a new field.
Here is the source code for edit in which where the displaying of data happens (Edit.cshtml)
<div class="nutrition-field">
@for (int i = 0; i < Model.NutritionFacts.Count(); i++ )
{
<div class="nutrition-div form-group row">
<div class="col-lg-4">
<label>Nutrients name</label>
<input type="text" class="nutrients-name form-control" asp-for="@Model.NutritionFacts.ToList()[i].NutritionName">
<span asp-validation-for="@Model.NutritionFacts.ToList()[i].NutritionName" class="text-danger"></span>
</div>
<div class="col-lg-4">
<label>Per 100g</label>
<input type="text" class="gram form-control" asp-for="@Model.NutritionFacts.ToList()[i].Gram">
<span asp-validation-for="@Model.NutritionFacts.ToList()[i].Gram" class="text-danger"></span>
</div>
<div class="col-lg-4">
<label>% Daily value</label>
<div class="d-flex">
<input type="text" class="percentage-value form-control mr-5 w-50" asp-for="@Model.NutritionFacts.ToList()[i].PercentageDailyValue">
<span asp-validation-for="@Model.NutritionFacts.ToList()[i].PercentageDailyValue" class="text-danger"></span>
<a href="javascript:void(0)" class="delete-btn btn btn-fixed-height btn-secondary font-weight-bold font-size-sm px-8">
Delete
</a>
</div>
</div>
</div>
}
</div>
And here is the javascript for the add button:
$(addNutrition).click(function() {
$.ajax({
url: "/Product/AddNutrition",
cache: false,
success: function (html) {
$('.nutrition-field').append(html)
}
});
});
When you click the button it will go to the controller and here is the code for the Add Nutrition Controller:
public IActionResult AddNutrition()
{
return PartialView("~/Views/Product/PartialViews/_AddNutrition.cshtml", new ProductNutritionFactsDto());
}
Here is the Partial View:
@model ProjectName.Dto.ProductNutritionFactsDto
<div class="nutrition-div form-group row">
<div class="col-lg-4">
<label>Nutrients name</label>
<input type="text" class="nutrients-name form-control" asp-for="NutritionName">
<span asp-validation-for="NutritionName" class="text-danger"></span>
</div>
<div class="col-lg-4">
<label>Per 100g</label>
<input type="text" class="gram form-control" asp-for="Gram">
<span asp-validation-for="Gram" class="text-danger"></span>
</div>
<div class="col-lg-4">
<label>% Daily value</label>
<div class="d-flex">
<input type="text" class="percentage-value form-control mr-5 w-50" asp-for="PercentageDailyValue">
<span asp-validation-for="PercentageDailyValue" class="text-danger"></span>
<a href="javascript:void(0)" class="delete-btn btn btn-fixed-height btn-secondary font-weight-bold font-size-sm px-8">
Delete
</a>
</div>
</div>
</div>
It's working but I wanted it to be inside the for loop so the counting of index will continue and it will have a unique ID. What is happening is it is creating a new model and the validations are not working when adding many fields.
I would really appreciate your help everyone. Thank you!