0

I have following data to send ajax call to the controller action:

var data = {
    "PersonalInfo": [
        {      
            "FirstName": "Leonel",
            "LastName": "Messi"     
        },
        {
            "FirstName": "Cristiano",
            "LastName": "Ronaldo" 
        }
    ]
};

Ajax call method:

$.ajax({
    url: "/Home/Create",
    type: "POST",
    dataType: "json",
    data: (JSON.parse(data)).PersonalInfo,
    success: function (response) { }                       
});

Controller Action:

public ActionResult Create(FormCollection formCollection)
{
    base.Create(formCollection);
}

In server side, I need array/list of FormCollection, e.g.:

formCollection[0] = {      
    "FirstName": "Leonel",
    "LastName": "Messi"     
}

formCollection[1] = {      
    "FirstName": "Cristiano",
    "LastName": "Ronaldo"      
}

Or something like following code so that I can call base.Create(formCollection) method multiple times for multiple personal info.

public ActionResult Create(List<FormCollection> formCollection)
{

}

But it is not possible, are there any alternative for this context?

1
  • 1
    Why on earth are you using FormCollection? Use a model with FirstName and LastName properties and bind to a collection of your model - public ActionResult Create(List<Person> model) Commented Oct 16, 2015 at 21:52

1 Answer 1

1

Do you have to use formcollection?

Visual studio has a special menu option selection that can consume a JSON object and output a class that the modelbinder maps to. Copy your expected JSON result (data object).

Go to a class file, then go to visual studio's menu bar, Edit => Paste Special => Paste JSON as classes.

Use the resulting classes as the argument to your api controller action call and they should map.

Sign up to request clarification or add additional context in comments.

1 Comment

I need to call base.Create(formCollection) method. If I create class from Json object, then cannot call base.Create(formCollection). Because base.Create(className) shows error message. Thank you for your reply.......

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.