ASP.NET MVC 5.2中如何保证基类属性先于派生类属性完成填充
根因说明
ASP.NET MVC 默认的DefaultModelBinder模型绑定器,会通过反射获取目标类型的所有公共可写属性,按字母顺序进行赋值。你当前的问题是由于派生类的Prop4排序在基类的Prop1、Prop2之前,绑定Prop4时触发了 getter 中的数据加载逻辑,此时Prop1、Prop2还未完成赋值,因此拿到的是默认值。
解决方案
方案1:调整属性设计(最推荐)
属性 getter 设计规范要求避免包含重IO逻辑、副作用逻辑或依赖其他属性状态的逻辑,因此建议将数据加载逻辑从Prop4的getter中剥离,改为显式调用的方法,模型绑定阶段不会触发该逻辑,等绑定完成后再手动执行即可。
修改后的类代码:
public class BaseClass { public string Prop1{get;set;} public string Prop2{get;set;} } public class DerivedClass : BaseClass { public string Prop3 {get;set;} // 改为普通自动属性 public IList<datamodel> Prop4 {get;set;} // 新增显式加载方法 public void LoadRelatedData() { if(Prop4 == null) { Prop4 = getdata(this.Prop1, this.Prop2); } } }
在Action中使用:
[HttpPost] public ActionResult Submit(DerivedClass model) { // 模型绑定已完成,所有属性都已赋值,此时加载数据正常 model.LoadRelatedData(); // 后续业务逻辑 }
方案2:自定义模型绑定器控制绑定顺序
如果必须保留原有属性设计,可以自定义模型绑定器强制先绑定基类的所有属性,再绑定派生类的属性,保证Prop1、Prop2先完成赋值。
自定义绑定器代码:
public class DerivedClassBinder : DefaultModelBinder { protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) { // 优先绑定基类所有属性 var baseProps = TypeDescriptor.GetProperties(typeof(BaseClass)).Cast<PropertyDescriptor>(); foreach (var prop in baseProps) { base.BindProperty(controllerContext, bindingContext, prop); } // 再绑定派生类独有的属性 var derivedProps = TypeDescriptor.GetProperties(typeof(DerivedClass)) .Cast<PropertyDescriptor>() .Where(p => !baseProps.Any(b => b.Name == p.Name)); foreach (var prop in derivedProps) { base.BindProperty(controllerContext, bindingContext, prop); } } }
注册绑定器(在Global.asax.cs的Application_Start方法中添加,.NET Core版本则在Program.cs中注册):
ModelBinders.Binders.Add(typeof(DerivedClass), new DerivedClassBinder());
方案3:将Prop4改为方法
直接把Prop4改为普通方法GetProp4Data(),模型绑定只会处理属性,不会主动调用方法,你在需要使用该数据的时候主动调用方法即可,此时基类属性已经完成赋值。
内容的提问来源于stack exchange,提问作者Richard H
相关产品推荐
相关产品推荐

