7

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

Kieron
  • 26,748
  • 16
  • 78
  • 122

1 Answers1

13

Sure. Use:

 public ActionResult Create([Bind(Prefix="Resource")]Resource resource)
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 2
    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 Aug 14 '09 at 13:29
  • 3
    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 Aug 14 '09 at 15:11