我有一个视图模型(让称之为HouseVM),但它包含了它(KitchenVM)内的另一个视图模型。我已经创建了KitchenVM自定义模型粘合剂。现在,我创建HouseVM ModelBinder的。我怎样才能访问模型结合我为KitchenVM的HouseVM模型粘合剂中已经做了什么?

请注意:我看到这个帖子

有帮助吗?

解决方案

选项#1

您可以有你的模型绑定的HouseVM从为KitchenVM定制粘合剂继承。这将允许厨房VM(或相关)属性)的结合仍然受到粘结剂的约束。是这样的:

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
    }
}

选项#2

此信息由麦博加德可以实现您各种定制模型粘合剂另一个很好的方法,允许每个类型结合其适当的模型。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top