Previously I was using @html.EditorFor(model=>model.Degree_name) but now I want to create dynamic forms as many times as the user clicks on add more button. So, I did that using html input controls and using javascript now my issue is I want to get the values which I insert into these textboxes in my controller. Previously I was passing model object in my controller which was working fine but now as my input controls are not model binded so how to get their values in controller using model object?
Previous code:
@Html.EditorFor(model => model.educations.School, new { htmlAttributes = new { placeholder = "Enter School Name", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.educations.School, "", new { @class = "text-danger" })
New Code:
<input name="School" id="School" type="text" value="" placeholder="Enter School Name" class="form-control" />
My controller method is below:
public ActionResult Create(Registration registration, HttpPostedFileBase upload,FacultyViewModel vm)
facultyViewModel was getting values previously.
From what it sounds you want to get a list of your model FacultyViewModel as a parameter. Change controller method signature to one below so vm is a collection of FacultyViewModels.
Controller
To have list of models passed to the controller parameter the html needs to look like this where every time you click add it adds the input tags with incremented index in name attribute.
Tested with what everything below.
Controller
cshtml
Your class FacultyViewModel needs to have a parameterless constructor.
Test Class