Binding and Posting a Collection of Custom types in ASP.NET MVC.

In ASP.NET MVC we can simply bind and post a custom type of collection. Here I am demonstrating a sample application for implementing this.
Create a sample class of type Person
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

Create a controller with two action such as Index for loading a List and Register for posting List
public class PersonController : Controller
{
public ActionResult Index()
{
var persons = new List()
{
new Person {FirstName = "Jameel", LastName = "VM"},
new Person {FirstName = "Jasin", LastName = "VM"}
};
return View(persons);
}
[HttpPost]
public ViewResult Register(List persons )
{
return View();
}
}
}

Index view code for looping the person collection and also I had put a form tag which map to Register action for posting the edited collection

code

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

When we click the register button the form will post the collection of values to Register action like below

1

2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s