views:

56

answers:

1

I have a viewmodel (lets call it HouseVM) but it contains another viewmodel inside of it (KitchenVM). I've already created a custom model binder for KitchenVM. Now I'm creating the HouseVM modelbinder. How can I access the model binding I've already done for KitchenVM within the HouseVM model binder?

NOTE: I have seen this post

A: 

Option # 1

You could have your model binder for the HouseVM inherit from your custom binder for the KitchenVM. This would allow binding of Kitchen VM (or related) properties) to still be bound by that binder. Something like:

public class HouseViewModelBinder : KitchenViewModelBinder
{
    protected override void BindProperty( ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor )
    {
        if (propertyDescriptor.PropertyType == typeof(KitchenVM))
        {
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
        // bind the other properties here
    }
}

Option # 2

This post by Jimmy Bogard may be another good way to implement your various customized model binders, allowing each type to bind to its appropriate model.

Chris Melinn