Jonn
Jonn

Reputation: 4661

Posting a Grid's data from view to the Controller in ASP.NET MVC

How do you post a grid's data from the View to the Controller? I would like to find out how to do it so that the model, when posted to the controller, would bring the grid data with it.

If I have this

public class ViewModel
{
    public virtual string Name { get; set; }
    public virtual List<DetailModel> Details { get; protected set; }
}

and I'm displaying it on a grid. I want to be able to take it when I post ViewModel back to the controller like so

[HttpPost]
public void Save(ViewModel model)
{
    repository.SaveDetails(model.Details);
}

I'm currently using MVCContrib's grid, but should a snippet be able to do this, any other grid implementation should be able to apply it too even MVC's default Detail template.

Upvotes: 0

Views: 1993

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

In order to show this data to the grid you must have fetched it from somewhere. So why posting it? Simply send some id and re-fetch it from the same datasource. On the other hand if your grid allows the user to modify the data, you would normally use an HTML <form> with input fields.

As far as binding is concerned here's a blog post which explains how the request parameters should be named in order to be able to successfully reconstruct the view model in the post action.

Upvotes: 2

Related Questions