asp.net-mvc – DataAnnotationsModelBinder如何使用自定义ViewModel?
|
我正在尝试使用 DataAnnotationsModelBinder,以便在ASP.NET MVC中使用数据注释进行服务器端验证. 只要我的ViewModel只是一个具有直接属性的简单类,一切都可以正常工作 public class Foo
{
public int Bar {get;set;}
}
但是,DataAnnotationsModelBinder在尝试使用复杂的ViewModel时会导致NullReferenceException,例如 public class Foo
{
public class Baz
{
public int Bar {get;set;}
}
public Baz MyBazProperty {get;set;}
}
对于渲染多个LINQ实体的视图来说,这是一个很大的问题,因为我更喜欢使用包含多个LINQ实体而不是无类型ViewData数组的自定义ViewModel. 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可以正常使用这样的hiearchies,所以我认为DataAnnotationsModelBinder也应该如此. 第二次编辑:看起来这确实是DataAnnotationsModelBinder中的一个错误.但是,希望在下一个ASP.NET MVC框架版本发布之前可以修复它.有关详细信息,请参见this forum thread. 解决方法我今天遇到了同样的问题.像我自己一样,我没有将我的View直接绑定到我的模型,而是使用一个中间的ViewDataModel类,它包含Model的一个实例以及我想发送给视图的任何参数/配置.我最终修改了DataAnnotationsModelBinder上的BindProperty来绕过NullReferenceException,我个人不喜欢只有绑定它们才有效(见下面的原因). 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,newPropertyValue)) {
OnPropertyValidated(controllerContext,newPropertyValue);
}
/ |
- asp.net-mvc – 如何为未经过身份验证的用户隐藏我的菜单?
- asp-classic – 浏览器似乎忽略了响应缓存控制指令
- asp.net-mvc – 将会话永久保持为stackoverflow
- asp.net-mvc – asp.net mvc未经授权的回复是空白页吗?
- asp.net – Azure WebJob超时配置设置
- asp.net-mvc-4 – 未找到入口点异常
- asp.net-core – RemoteIpAddress始终为null
- asp.net中js+jquery添加下拉框值和后台获取示例
- asp.net-mvc – ASP.NET MVC Master Detail Entry表单
- asp-classic – 如何在asp经典中触发异步调用并忽略响应?
- asp.net-mvc – 如何将XML作为POST传递给ASP MVC
- 在ASP.NET中添加动态控件,1.1和2.0之间有区别吗?
- asp.net – 缓存策略,输出缓存与数据缓存或两者兼
- asp.net-mvc-3 – Azure网站上的RavenDb – 访问
- ASP.NET成员:拒绝用户阻止CSS,页面无法正确呈现
- asp.net-mvc-3 – 实体框架|代码优先|从CultureI
- 如何在IIS 7.5上预热ASP.NET MVC应用程序?
- 如何在asp.net用户控件中使用jQuery ajax?
- asp.net-mvc – jQuery脚本包含在mvc 4模板的页面
- asp.net – 单元测试和Log4net
