开发者

Binding ViewModels With Other ViewModelBinders

I have a viewmodel (lets call it HouseVM) but it contains another viewmodel inside of it (KitchenVM). I've already created a custom model binde开发者_运维技巧r 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜