I’m fetching an array of viewmodels from my controller using jquery+json. I then build a form where each row in a table represents one viewmodel.
My question is: How should I name each form element so that I can get it to my controller action like this:
public ActionResult Update(MyViewModel[] models)
{
}
Edit: I’m using jquery-tmpl to generate the form, and I’m also trying to figure out how to get an index variable in it (if that’s needed for the form generation).
I managed to get it working.
My jquery template:
Method that loads the template:
In other words:
To get a
YourModel[] itemsargument in your controller action you need to name the items asitems[0].MyProperty' where0` should correspond to the index in the array.To get an index in a jquery template, just use pass a method in the options to the template function. I’m using a slightly modified version of the answer found here. Passing the item as done in that answer is not necessary as
thispoints on the current item.