views:

725

answers:

1

Hi,

I've got a series of views, each are typed to have their own ViewModel class which contains everything they need to display themselves, for example:

public class CreateResourceViewModel
{
     public Project Parent { get; set; }
     public SelectList Categories { get; set; }
     public Resource Resource { get; set; }
}

The post action method for this I'd like to use would look like this:

[AcceptVerbs (HttpVerbs.Post)]
public ActionResult Create (Resource resource)
{
   // Update code...
}

Notice that the only object I'm interested in is the Resource property of the CreateResourceViewModel, not the CreateResourceViewModel itself. Everything else is just gravy for for the user, what they're updating is the resource class...

Is this possible within the MVC Framework (even if it's v2 CTP)?

Thanks all

+8  A: 

Sure. Use:

 public ActionResult Create([Bind(Prefix="Resource")]Resource resource)
Craig Stuntz
Hi, that didn't work - resource is null upon submit...
Kieron
Then either your posted form does not contain the information required for the default model binder to materialize a Resource, or your form keys don't match the presentation model you showed before. Look at the posted form in Firebug or Fiddler. If you can't figure out the problem, post both that and the Resource type declaration here.
Craig Stuntz
Ah ha! I was manually putting the data in the html (Html.TextBox ("Name", Model.Resource.Name)), I changed it to Html.TextBox ("Resource.Name") and all was good. THanks Craig!
Kieron