-2

Main question: Is it possible to post a list of model using partial view in asp.net mvc?

Example: Lets assume a class as Student having 2 properties(RollNo(int) and Name(string))

public class Student 
{ 
     public int RollNo { get; set; }
     public string Name { get; set; }
}

Controller having post method where I want list of students from partial view

[HttpPost]
public ActionResult PostListOfStudents(List<Student> lstStudents)
{
    //some logic
}

1 Answers1

1

Yes sure you can.

Here is partial view model:

@model List<Student>

You only have to iterate over your list model like below:

@Html.BeginForm("action", "controller", FormMethod.Post)
{
    if (Model != null) 
    {
       for (var i = 0; i < Model.Count; i++) 
       {
           @Html.TextBoxFor(m => Model[i].Name)
           @Html.TextBoxFor(m => Model[i].RollNo)
       }
       <input type="submit" value="Post Data" /> 
    }
}

You can pass the list of Students in partial view as below:

@Html.Partial("_partialview", listOfStudents);

Then at your post action you will get the list of student. Just be sure to use proper input extension methods in your case.

Rey
  • 3,663
  • 3
  • 32
  • 55