Monday, March 20, 2017

In this example we are going to retrieve a list of objects from the front end to controller.

Lets say we are allowing the end user to enter their favorite x number of books as in the below image

The challenge is how to retrieve a list of objects from the front end to back end specially when the of object count is dynamic.

First add a controller method

Then add the view for that method

Add a class for Book list. In here we add a class called book and we use it in a list.

We have to modify the view to include the input fields to get the data from end user

Note that the input fields are named using an indexing method. When you run the project and inspect this page with Developer Tools you can see how the input names are rendered in to the HTML page.

This index should be a zero based and unbroken series of integer numbers. 

The code is basically generating names for the text boxes matching the following convention:
Books[n].[Model _Property_Name]

And in the controller we should define the parameter to match the Name in the view as shown below

Now we can fill the books detail in front end and then click on the submit.

In the debug mode you can see entered data are available as a Book list :)

Original post link here
You can download the sample project here

Post a Comment: