3

I have the following ViewModels :

public class MyViewModel
{
  public BaseViewModel mySubViewModel;
}

public class ChildViewModel: BaseViewModel
{}

I then create a MyViewModel model, which contains a property of type ChildViewModel. In the View, it is displayed just fine.

Then I hit the save button to submit changes to my Model and I call the following controller:

    [HttpPost]
    public ActionResult Edit(MyViewModel model)
    {
        return null;
    }

To my surprise, the property mySubViewModel is now of type BaseViewModel instead of ChildViewModel ! I have no idea what's going on here. What am I doing wrong ?

Sam
  • 13,934
  • 26
  • 108
  • 194

1 Answers1

7

To my surprise, the property mySubViewModel is now of type BaseViewModel instead of ChildViewModel ! I have no idea what's going on here. What am I doing wrong ?

You shouldn't be surprised by that. You are doing nothing wrong. This is by design. The default model binder sees that your controller action is taking a MyViewModel argument and is attempting to bind the contents of the POST request to it. The default model binder has absolutely no way of knowing that you might have written some derived classes (such as ChildViewModel in your case) and that you want those derived classes to be used instantiated here. The fact that you have passed this concrete view model instance from your GET action to the view has no influence to the POST action. Think of it in terms of HTTP and what the default model binder sees. Think for example that this POST action could be invoked from some completely different client and not from a form submission. Could be for example an iPhone application performing an HTTP request to the POST action. See, now it makes perfect sense. The default model binder can only see the payload of the POST request and the type you specified as action argument. And that's what he does => it instantiates this types and binds its properties from the POST payload data.

So one possibility is to write a custom model binder that will instantiate the concrete instance of your view model you wish. I have exemplified such a custom model binder at this post. In this example I have included a hidden field inside the form that will contain the concrete type of the view model that we would like to be instantiated and then the custom model binder simply uses this information to create this type at runtime.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • An alternative would just be to change the type of `mySubViewModel` on `MyViewModel` to `ChildViewModel`. – Bennor McCarthy Feb 02 '13 at 22:59
  • Yeah sure, that would also work. But I guess that the OP doesn't know the exact runtime type to be used and doesn't want it to be hardcoded, that's why he used the base type as parameter to his action. – Darin Dimitrov Feb 02 '13 at 22:59
  • exactly right, I only know about the type at runtime. I won't have time today to check out your link, but I'll definitely get back to you once I've read the article. Thanks a lot for your help. – Sam Feb 04 '13 at 08:01
  • actually I found the time to look at your link. Model binder is what I should use indeed. I have just one problem with the code you suggested in your post. You pass a modelType parameter to the CreateModel method, but this parameter is never used. Is there a typo somewhere in the code ? – Sam Feb 04 '13 at 12:53
  • No, it's not a typo at all. This parameter is used by the custom model binder to know the concrete type it should instantiate. Take a look at the following line in the model binder: `var typeValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".ModelType");`. Notice the suffix? Without this hidden value the model binder wouldn't know which concrete view model it should instantiate which was your original problem in the first place. – Darin Dimitrov Feb 04 '13 at 13:04
  • Well, still I don't understand. The definition of the constructor is protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType). However the variable modelType is not used. Instead you've hardcoded the string ".ModelType". Can you shed some light please ? :) – Sam Feb 04 '13 at 18:58
  • Well, I've implemented it and it works fine nevertheless :) That's great ! I'm a bit confused though (cf. my previous comment) – Sam Feb 04 '13 at 19:19
  • You were talking about the `modelType` variable that the `CreateModel` method is talking? Sorry I didn't understand that. This variable is not used. It is part of the signature of the method that I override. – Darin Dimitrov Feb 04 '13 at 20:05