I have the following ViewModel which I fill with some predefined data. One of that predefined data is a List of ApplicationParameter ViewModels which is set to initially display n number of ApplicationParameters.
What I wanted to achieve is to allow user to add additional number of ApplicationParameters ViewModels on a button click which I achieved by calling PartialView with the ApplicationParameter code. As expected controls do add automatically but they aren't recognized in the ViewModel as they don't have the correct naming (as they are being nested).
What to do to make dynamically added controls visible to the ViewModel returned on the POST.
ViewModels
public class ApplicationVM
{
public int idApplication { get; set; }
public int idCompany { get; set; }
public string Company { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Link { get; set; }
public string Photo { get; set; }
public List<ApplicationParameterVM> ApplicationParameters { get; set; }
}
public class ApplicationParameterVM
{
public string Name { get; set; }
public bool Keep { get; set; }
}
Action view (code snippet)
<tbody id="editorRows">
@for (int i = 0; i < Model.ApplicationParameters.Count(); i++)
{
<tr>
<td>
@Html.TextBoxFor(m => Model.ApplicationParameters[i].Name, new { @class = "form-control" })
@Html.CheckBoxFor(m => Model.ApplicationParameters[i].Keep)
</td>
</tr>
}
</tbody>
Action view for adding ApplicationParameterVM
@model AccessMarketmind.Areas.Administration.ViewModels.ApplicationParameterVM
@{
Layout = null;
}
<tr class="application-parameters">
<td>
@Html.TextBoxFor(m => Model.Name, new { @class = "form-control" })
@Html.CheckBoxFor(m => Model.Keep)
</td>
</tr>
I know this one looks trivial and could be easily done using Javascript, but the thing is that I have more complicated ViewModels (read: heavily nested) in which Javascript isn't an option.