In my Entity Framework .NET MVC app I want to append a new row to a table with JS but I want to be able to do data validation. Therefore I need to use Html.EditorFor.
This is the current JS code:
$("#add_btn").on('click', function (e) {
var table = $("#element_table");
var idx = $(table).find("tbody>tr").length;
var htmlToAppend = `<tr>
<td><input name="item.Elements[${idx}].Name" id="Elements[${idx}].Name" /></td>
<td><input name="item.Elements[${idx}].Quantity" id="Elements[${idx}].Quantity" /></td>
</tr>`;
$("#element_table").append(htmlToAppend);
});
I want to have something like this:
var htmlToAppend = `<tr>
<td> @Html.EditFor(model => model.Elements[${idx}].Name </td>
<td> @Html.EditFor(model => model.Elements[${idx}].Quantity</td>
</tr>`
But I can't find the correct syntax.