Asp.net mvc 2 如何在模型绑定过程中设置模型属性?

Asp.net mvc 2 如何在模型绑定过程中设置模型属性?,asp.net-mvc-2,Asp.net Mvc 2,我的许多模型对象继承自一个名为AuditedEntity的类,该类跟踪对象的更改。我想让从AuditedEntity继承的模型对象在模型绑定过程中构造时自动填充相应的字段。我一直在研究对默认模型活页夹进行子分类,但运气不太好 有人能给我指出正确的方向吗 这些属性是否由已知值或来自良好来源的值填充。或者这些属性是否填充了依赖于表单/路线/查询等中的值的值 对DefaultModelBinder进行子类化应该很好,例如: public class MyModel { public string

我的许多模型对象继承自一个名为AuditedEntity的类,该类跟踪对象的更改。我想让从AuditedEntity继承的模型对象在模型绑定过程中构造时自动填充相应的字段。我一直在研究对默认模型活页夹进行子分类,但运气不太好


有人能给我指出正确的方向吗

这些属性是否由已知值或来自良好来源的值填充。或者这些属性是否填充了依赖于表单/路线/查询等中的值的值

对DefaultModelBinder进行子类化应该很好,例如:

public class MyModel
{
  public string Forename { get; set }

  public string SomeSpecialProperty { get; set; }
}

public MyModelBinder : DefaultModelBinder
{
  public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  {
    var model = (MyModel)base.BindModel(controllerContext, bindingContext);

    model.SomeSpecialProperty = // Do something here...

    return model;
  }
}

ModelBinder.Binders[typeof(MyModel)] = new MyModelBinder();

到目前为止,你发现了什么?

我最终不得不依赖所有绑定的数据。这就是我最终使用的解决方案。将代码放在OnModelUpdated中允许我依赖已设置的其他属性

public class AuditedEntityModelBinder : DefaultModelBinder
{

    protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType.IsSubclassOfGeneric(typeof(AuditedEntity<>)))
        {
            string name = controllerContext.HttpContext.User.Identity.Name;

            if(!name.IsNullOrEmpty())
            {
                if((bool) bindingContext.ModelType.GetProperty("IsNew").GetValue(bindingContext.Model, null))
                {
                    bindingContext.ModelType.GetProperty("CreatedBy").SetValue(bindingContext.Model, name, null);
                    bindingContext.ModelType.GetProperty("Created").SetValue(bindingContext.Model, DateTime.Now, null);
                }
                else
                {
                    bindingContext.ModelType.GetProperty("ModifiedBy").SetValue(bindingContext.Model, name, null);
                    bindingContext.ModelType.GetProperty("Modified").SetValue(bindingContext.Model, DateTime.Now, null);
                }

            } 
        }

        base.OnModelUpdated(controllerContext, bindingContext);
    }
}
公共类AuditedEntityModelBinder:DefaultModelBinder
{
模型更新时受保护的覆盖无效(ControllerContext ControllerContext、ModelBindingContext bindingContext)
{
if(bindingContext.ModelType.IsSubclassOfGeneric(typeof(AuditedEntity)))
{
字符串名称=controllerContext.HttpContext.User.Identity.name;
如果(!name.IsNullOrEmpty())
{
if((bool)bindingContext.ModelType.GetProperty(“IsNew”).GetValue(bindingContext.Model,null))
{
bindingContext.ModelType.GetProperty(“CreatedBy”).SetValue(bindingContext.Model,name,null);
bindingContext.ModelType.GetProperty(“已创建”).SetValue(bindingContext.Model,DateTime.Now,null);
}
其他的
{
bindingContext.ModelType.GetProperty(“ModifiedBy”).SetValue(bindingContext.Model,name,null);
bindingContext.ModelType.GetProperty(“修改”).SetValue(bindingContext.Model,DateTime.Now,null);
}
} 
}
base.OnModelUpdated(controllerContext、bindingContext);
}
}

谢谢您的回答。我真的弄明白了。我也意识到我没有很好地澄清我的问题。谢谢你的回答,我有一些额外的要求,所以我会在下面发布我的解决方案,但是会给你答案。