我正在尝试使用 DataAnnotationsModelBinder为了在 ASP.NET MVC 中使用数据注释进行服务器端验证。
只要我的 ViewModel 只是一个具有直接属性的简单类,例如
,一切都会正常工作public class Foo
{
public int Bar {get;set;}
}
但是,当尝试使用复杂的 ViewModel
时,DataAnnotationsModelBinder
会导致 NullReferenceException
,例如
public class Foo
{
public class Baz
{
public int Bar {get;set;}
}
public Baz MyBazProperty {get;set;}
}
对于呈现多个 LINQ 实体的 View 来说,这是一个大问题,因为我确实更喜欢使用包含多个 LINQ 实体的自定义 ViewModel
,而不是非类型化的 ViewData 数组。
DefaultModelBinder
没有这个问题,所以它看起来像是 DataAnnotationsModelBinder
中的一个错误。有什么解决办法吗?
编辑:一个可能的解决方法当然是在 ViewModel 类中公开子对象的属性,如下所示:
public class Foo
{
private Baz myBazInstance;
[Required]
public string ExposedBar
{
get { return MyBaz.Bar; }
set { MyBaz.Bar = value; }
}
public Baz MyBaz
{
get { return myBazInstance ?? (myBazInstance = new Baz()); }
set { myBazInstance = value; }
}
#region Nested type: Baz
public class Baz
{
[Required]
public string Bar { get; set; }
}
#endregion
}
#endregion
但我不想编写所有这些额外的代码。 DefaultModelBinder
可以很好地处理此类层次结构,因此我认为 DataAnnotationsModelBinder
也应该如此。
第二次编辑:看起来这确实是DataAnnotationsModelBinder
中的一个错误。不过,希望在下一个 ASP.NET MVC 框架版本发布之前解决这个问题。请参阅this forum thread了解更多详情。
最佳答案
我今天遇到了完全相同的问题。像您一样,我不会将 View 直接绑定(bind)到模型,而是使用中间 ViewDataModel 类,该类保存模型的实例以及我想要发送到 View 的任何参数/配置。
我最终修改了 DataAnnotationsModelBinder 上的 BindProperty
以规避 NullReferenceException
,而且我个人不喜欢仅在属性有效时才绑定(bind)属性(请参阅下面的原因) .
protected override void BindProperty(ControllerContext controllerContext,
ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor) {
string fullPropertyKey = CreateSubPropertyName(bindingContext.ModelName, propertyDescriptor.Name);
// Only bind properties that are part of the request
if (bindingContext.ValueProvider.DoesAnyKeyHavePrefix(fullPropertyKey)) {
var innerContext = new ModelBindingContext() {
Model = propertyDescriptor.GetValue(bindingContext.Model),
ModelName = fullPropertyKey,
ModelState = bindingContext.ModelState,
ModelType = propertyDescriptor.PropertyType,
ValueProvider = bindingContext.ValueProvider
};
IModelBinder binder = Binders.GetBinder(propertyDescriptor.PropertyType);
object newPropertyValue = ConvertValue(propertyDescriptor, binder.BindModel(controllerContext, innerContext));
ModelState modelState = bindingContext.ModelState[fullPropertyKey];
if (modelState == null)
{
var keys = bindingContext.ValueProvider.FindKeysWithPrefix(fullPropertyKey);
if (keys != null && keys.Count() > 0)
modelState = bindingContext.ModelState[keys.First().Key];
}
// Only validate and bind if the property itself has no errors
//if (modelState.Errors.Count == 0) {
SetProperty(controllerContext, bindingContext, propertyDescriptor, newPropertyValue);
if (OnPropertyValidating(controllerContext, bindingContext, propertyDescriptor, newPropertyValue)) {
OnPropertyValidated(controllerContext, bindingContext, propertyDescriptor, newPropertyValue);
}
//}
// There was an error getting the value from the binder, which was probably a format
// exception (meaning, the data wasn't appropriate for the field)
if (modelState.Errors.Count != 0) {
foreach (var error in modelState.Errors.Where(err => err.ErrorMessage == "" && err.Exception != null).ToList()) {
for (var exception = error.Exception; exception != null; exception = exception.InnerException) {
if (exception is FormatException) {
string displayName = GetDisplayName(propertyDescriptor);
string errorMessage = InvalidValueFormatter(propertyDescriptor, modelState.Value.AttemptedValue, displayName);
modelState.Errors.Remove(error);
modelState.Errors.Add(errorMessage);
break;
}
}
}
}
}
}
我还修改了它,以便它始终绑定(bind)属性上的数据,无论它是否有效。这样我就可以将模型传递回 View ,而无需将无效属性重置为 null。
Controller 摘录
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(ProfileViewDataModel model)
{
FormCollection form = new FormCollection(this.Request.Form);
wsPerson service = new wsPerson();
Person newPerson = service.Select(1, -1);
if (ModelState.IsValid && TryUpdateModel<IPersonBindable>(newPerson, "Person", form.ToValueProvider()))
{
//call wsPerson.save(newPerson);
}
return View(model); //model.Person is always bound no null properties (unless they were null to begin with)
}
我的模型类(Person)来自网络服务,所以我不能直接在它们上添加属性,我解决这个问题的方法如下:
嵌套 DataAnnotations 的示例
[Validation.MetadataType(typeof(PersonValidation))]
public partial class Person : IPersonBindable { } //force partial.
public class PersonValidation
{
[Validation.Immutable]
public int Id { get; set; }
[Validation.Required]
public string FirstName { get; set; }
[Validation.StringLength(35)]
[Validation.Required]
public string LastName { get; set; }
CategoryItemNullable NearestGeographicRegion { get; set; }
}
[Validation.MetadataType(typeof(CategoryItemNullableValidation))]
public partial class CategoryItemNullable { }
public class CategoryItemNullableValidation
{
[Validation.Required]
public string Text { get; set; }
[Validation.Range(1,10)]
public string Value { get; set; }
}
现在,如果我将表单字段绑定(bind)到 [ViewDataModel.]Person.NearestGeographicRegion.Text
和 [ViewDataModel.]Person.NearestGeographicRegion.Value
,ModelState 就会开始正确验证它们并且 DataAnnotationsModelBinder 也可以正确绑定(bind)它们。
这个答案不是确定的,这是我今天下午挠头的结果。 尽管它通过了 the project 中的单元测试,但它还没有经过适当的测试。 Brian Wilson 开始了我自己的大部分有限测试。为了真正解决这个问题,我很想听听Brad Wilson关于此解决方案的想法。
关于asp.net-mvc - DataAnnotationsModelBinder 如何与自定义 ViewModel 配合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/820468/