代码之家  ›  专栏  ›  技术社区  ›  RailRhoad

使用其他ViewModelBinder绑定ViewModels

  •  0
  • RailRhoad  · 技术社区  · 15 年前

    我有一个viewmodel(我们称之为HouseVM),但它包含另一个viewmodel(KitchenVM)。我已经为KitchenVM创建了一个自定义模型活页夹。现在我正在创建HouseVM模型绑定器。如何在HouseVM模型绑定器中访问我已经为KitchenVM完成的模型绑定?

    注:我看过 this post

    1 回复  |  直到 8 年前
        1
  •  0
  •   Chris Melinn    15 年前

    选项1

    您可以从KitchenVM的自定义活页夹继承HouseVM的模型活页夹。这将允许厨房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

    This post by Jimmy Bogard

    推荐文章