I want to dynamically add textboxes and bind it to my model, so when I post all data should be in HttpPost. Here is my model
public class JobViewModel
{
public JobViewModel()
{
JobDescriptions = new List<JobDescription>();
}
public int Id { get; set; }
[Required]
public string JobTitle { get; set; }
public List<JobDescription> JobDescriptions { get; set; }
}
Here is my Razor view
@using (Html.BeginForm("CreateJob", "Jobs", FormMethod.Post, new { @id = "jobFrm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Job</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.JobTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Add Description", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
<input id="btnAdd" type="button" class="btn btn-success" value="Add" />
</div>
............. more fields and submit button ............
I searched and tried different codes, they add textboxes while clicking ADD button, but that filled textboxes value are not binded to my model. Sometimes it add empty model, or none at all. Need help to bind values to my model