i want to add dynamic fields into a list of items that are contained in a IEnumerable in a model class.
I followed this tutorial where they say that you can put
$("#addItem").click(function() {
$.ajax({
url: this.href,
cache: false,
success: function(html) {$("#PuntosMaestrosItems").append(html); }
});
return false;
});
But when i post the form , the new added rows are not considered as part of the model. I am sure is because the names of the fields has their own id and name attributes , so they have to follow a pattern like the original ones(not added by js). Can you suggest a way to do that? Any tutorial? Any help? I am lost.
The code called in jquery
[HttpGet]
public PartialViewResult BlankFormModelConfigurarAreaItem()
{
return PartialView("FormModelConfigurarAreaItem", new FormModelConfigurarAreaItem { Agrupaciones = db.AgrupacionChequeos.ToList(), Seleccionado = true });
}
The model class containing the IEnumerable
public class FormModelCongifurarArea
{
public int AreaId { get; set; }
public IEnumerable<FormModelConfigurarAreaItem> PuntoMaestroItem{ get; set; }
}
Thw view of FormModelConfigurarArea
@using Inspecciona.Helpers
@model Inspecciona.Models.FormModelCongifurarArea
<h2>ConfigurarInformeAreaNuevo</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>FormModelCongifurarArea</legend>
<div class="editor-field">
@Html.HiddenFor(model => model.AreaId)
@Html.ValidationMessageFor(model => model.AreaId)
</div>
<table>
<thead>
<tr>
<th></th>
<th>AspectoAControlarPuntoChequeo</th>
<th>DescPuntoChequeo</th>
<th>Agrupacion Chequeo</th>
<th>Seleccionado</th>
</tr>
</thead>
</table>
<div id="PuntosMaestrosItems">
@Html.EditorFor(model => model.PuntoMaestroItem)
</div>
<p>
@Html.ActionLink("Add Punto chequeo", "BlankFormModelConfigurarAreaItem", null, new { id = "addItem" });
</p>
<p>
<input type="submit" value="Guardar" />
</p>
</fieldset>
}
And the Action of the Post in the controller
[HttpPost]
public ActionResult ConfigurarInformeArea(FormModelCongifurarArea formModelConfigurarArea ){
}
REMEMBER: the problem is that when i add a new row of type FormModelConfigurarAreaItem the [HttpPost] is not getting the fields in formModelConfigurarArea.PuntoMaestroItem by some names/ids generated in javascript are not fitting the expeted ones. Many thanks!! I will mark as answer the best answer