tags:

views:

433

answers:

4

Hi,

I'm just starting to play with asp.net mvc and I've got a very basic question:

If I have a view that shows information about System.Web.Mvc.ViewPage<Foo>, how can I get the actual object when passing the form values to my Edit Action on my FooController? By default I got this implementation of the Edit action:

public ActionResult Edit(int id, FormCollection collection)

Is there any way I could have another overload like this?

public ActionResult Edit(int id, Foo myObject)

Thanks.

EDITED:

Sorry guys, just a little bit more of background so you understand what I was thinking of doing....as I have my own framework to replay the changes I made on my entities I didn't need to re-query my entity and use the UpdateModel() method. What I ended up doing was just having a method like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Foo myObject)
{
}

I did not know but that works perfectly fine. Thanks to the answer which led me to this post: http://www.haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

A: 

You could potentially write some crazy type converter, however there are helpers methods to map the values from the FormCollection onto your Foo object.

EDIT: I think its implemented as an extension method and is called UpdateFrom(..)

tarn
A: 

The easiest way to implement your edit method is by using the UpdateModel method which is already included in the Controller base class like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{

    LinqEntity entity = MyRepository.GetEntity(id);
    UpdateModel(entity);

    //validate and save your entity here

}

Edit: I have also just started to learn ASP.NET MVC and found ScottGu's MVC Walkthrough very useful. It shows how to implement a simple MVC website (you can see it at nerddinner.com) and covers many run-of-the-mill topics like the one above.

Adrian Grigore
A: 

Related question:
http://stackoverflow.com/questions/267707/how-to-pass-complex-type-using-json-to-asp-net-mvc-controller

This will probably answer your needs.

Cherian
A: 

If you need some look at how-to do it right go to nerddinner.com and download the source code. This site is developed by Scott Gu, Phil Haack and others guy to show how to do MVC...

Davide Vosti