html input in razor view i have id and name attributes with them how to get their value in controller

2.8k Views Asked by At

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.

1

There are 1 best solutions below

3
mitchelll08 On

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

public ActionResult create(Registration registration, HttpPostedFileBase upload,
                           List<FacultyViewModel> vm) 
{    

} 

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.

<input name="FacultyViewModel[0].educations.School" type="text" />
<input name="FacultyViewModel[0].educations.Degree_Title" type="text"/>

<input name="FacultyViewModel[1].educations.School" type="text" />
<input name="FacultyViewModel[1].educations.Degree_Title" type="text" />

<input name="FacultyViewModel[2].educations.School" type="text" />
<input name="FacultyViewModel[2].educations.Degree_Title" type="text" />

Tested with what everything below.

Controller

public ActionResult postTest(List<Test> test)
{
    //I set break point here to view list contents
}

cshtml

<div class="container pt-5">
<div class="pt-5">
    @using (Html.BeginForm("postTest", "Admin", FormMethod.Post, new { @class = "myForm", @id = "CreatePostForm", enctype = "multipart/form-data" }))
    {
        <input type="text" name="Test[0].school" />
        <input type="text" name="Test[0].degree" />


        <input type="text" name="Test[1].school" />
        <input type="text" name="Test[1].degree" />

        <div class="form-group pt-3">
            <button type="submit" class="btn btn-dark">Post</button>
        </div>
    }
</div>

Your class FacultyViewModel needs to have a parameterless constructor.

Test Class

public class Test
{
    public Test(string s, string d)
    {
        this.School = s;
        this.Degree = d;
    }

    public Test()
    {
    }

    public string School { get; set; }
    public string Degree { get; set; }
}