The tile may not be very appropriate for my problem
I have a model:
public class MpgViewModel
{
public DSModel DSht { get; set; }
public List<CMModel> CMps{ get; set; }
}
The CMModel is:
public class CMModel
{
public int Id { get; set; }
public int DShtId { get; set; }
public int SCI { get; set; }
public string SCT { get; set; }
public string TCT { get; set; }
public bool Active { get; set; }
public DateTimeOffset LastUpdatedOn { get; set; }
public string LastUpdatedBy { get; set; }
}
Now I have a Form, which takes input for the DSModel and then, I want to have the inputs for the list of CMModel in the same form. There can be arbitrary number of CMModel objects that will be determined by the user. I want to add more objects to the list by the user, simply clicking an Add More button.
I have created a partial for the CMModel Inputs, that I want to append to the main form, whenever the user clicks Add More button.
@model CMModel
<div class="cmEntry">
<fieldset class="form-group">
<label for="sci">sci: </label>
<input type="number" asp-for="sci" class="form-control" id="sci" />
</fieldset>
<fieldset class="form-group">
<label for="sct">sct: </label>
<input type="text" asp-for="sct" class="form-control" id="sct" />
</fieldset>
<fieldset class="form-group">
<label for="tct">tct: </label>
<input type="text" asp-for="tct" class="form-control" id="tct" />
</fieldset>
<fieldset class="form-group">
<label for="active">Active: </label>
<select asp-for="Active" class="form-control" id="active">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</fieldset>
<fieldset class="form-group">
<label for="lastUpdatedOn">Last Updated On:</label>
<input type="date" asp-for="LastUpdatedOn" class="form-control" id="lastUpdatedOn" />
</fieldset>
</div>
What is the best way to approach this functionality. Should I just create this partial in jquery like $(#myDiv).append(" ....."); or should I send ajax requests to the controller to return the partial every time the button is clicked and append the result to he form. And will ASP.net be able to bind it properly when the form is submitted?
EDIT:
BeginCollectionItem can achieve what I am looking for, but it seems it is not available for asp.net core. I got an error in my partial view when I use
@using (Html.BeginCollectionItem("Entries"))
{
}
The Type HtmlHelper is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Mvc, Version= 2.0.0.0'