MVVM and ModelBinders in the ASP.NET MVC Framework
Asked Answered
F

1

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

Flann answered 14/8, 2009 at 11:58 Comment(0)
D
13

Sure. Use:

 public ActionResult Create([Bind(Prefix="Resource")]Resource resource)
Diaghilev answered 14/8, 2009 at 12:50 Comment(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.Diaghilev
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!Flann

© 2022 - 2024 — McMap. All rights reserved.