Odn*_*nxe 9 validation asp.net-mvc modelbinders modelstate data-annotations
我有一些关于自定义模型绑定,模型状态和数据注释的问题.
1)如果我的模型上有数据注释,那么在自定义模型绑定器中进行验证是否是多余的,因为这就是我认为数据注释的重点.
2)为什么我的控制器将模型状态视为有效,即使它不是,主要是我使Name属性为null或太短.
3)将自定义模型绑定器视为构造方法是否可以,因为这是他们提醒我的.
首先是我的模型.
public class Projects
{
[Key]
[Required]
public Guid ProjectGuid { get; set; }
[Required]
public string AccountName { get; set; }
[Required(ErrorMessage = "Project name required")]
[StringLength(128, ErrorMessage = "Project name cannot exceed 128 characters")]
[MinLength(3, ErrorMessage = "Project name must be at least 3 characters")]
public string Name { get; set; }
[Required]
public long TotalTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我使用自定义模型绑定器来绑定模型的某些属性.请不要介意它只是试图让它运行然后重构它是快速和肮脏的.
public class ProjectModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
if (bindingContext == null)
{
throw new ArgumentNullException("bindingContext");
}
var p = new Project();
p.ProjectGuid = System.Guid.NewGuid();
p.AccountName = controllerContext.HttpContext.User.Identity.Name;
p.Name = controllerContext.HttpContext.Request.Form.Get("Name");
p.TotalTime = 0;
//
// Is this redundant because of the data annotations?!?!
//
if (p.AccountName == null)
bindingContext.ModelState.AddModelError("Name", "Name is required");
if (p.AccountName.Length < 3)
bindingContext.ModelState.AddModelError("Name", "Minimum length is 3 characters");
if (p.AccountName.Length > 128)
bindingContext.ModelState.AddModelError("Name", "Maximum length is 128 characters");
return p;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我的控制器动作.
[HttpPost]
public ActionResult CreateProject([ModelBinder(typeof(ProjectModelBinder))]Project project)
{
//
// For some reason the model state comes back as valid even when I force an error
//
if (!ModelState.IsValid)
return Content(Boolean.FalseString);
//_projectRepository.CreateProject(project);
return Content(Boolean.TrueString);
}
Run Code Online (Sandbox Code Playgroud)
编辑
我在另一个stackoverflow问题上找到了一些代码但是我不确定在哪个点上我会将以下值注入到这个可能的解决方案中.
创建新对象时我想要注入的内容:
var p = new Project();
p.ProjectGuid = System.Guid.NewGuid();
p.AccountName = controllerContext.HttpContext.User.Identity.Name;
p.Name = controllerContext.HttpContext.Request.Form.Get("Name");
p.TotalTime = 0;
Run Code Online (Sandbox Code Playgroud)
如何将上述代码纳入以下内容(可能的解决方案):
public class ProjectModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType == typeof(Project))
{
ModelBindingContext newBindingContext = new ModelBindingContext()
{
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(
() => new Project(), // construct a Project object,
typeof(Project) // using the Project metadata
),
ModelState = bindingContext.ModelState,
ValueProvider = bindingContext.ValueProvider
};
// call the default model binder this new binding context
return base.BindModel(controllerContext, newBindingContext);
}
else
{
return base.BindModel(controllerContext, bindingContext);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果继承DefaultModelBinder,覆盖BindModel方法,调用base.BindModel方法然后进行手动更改(设置guid,帐户名和总时间),您会发现事情更容易.
1)完成验证是多余的.您可以编写代码来反映验证元数据,就像默认情况一样,或者只是删除数据注释验证,因为您没有在模型绑定器中使用它.
2)我不知道,似乎是正确的,您应该单步执行代码并确保您的自定义绑定器填充所有适用的规则.
3)这是一个肯定的工厂,但不是一个构造函数.
编辑:你不能更接近解决方案,只需在模型工厂功能中设置所需的属性
public class ProjectModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType == typeof(Project))
{
ModelBindingContext newBindingContext = new ModelBindingContext()
{
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(
() => new Project() // construct a Project object
{
ProjectGuid = System.Guid.NewGuid(),
AccountName = controllerContext.HttpContext.User.Identity.Name,
// don't set name, thats the default binder's job
TotalTime = 0,
},
typeof(Project) // using the Project metadata
),
ModelState = bindingContext.ModelState,
ValueProvider = bindingContext.ValueProvider
};
// call the default model binder this new binding context
return base.BindModel(controllerContext, newBindingContext);
}
else
{
return base.BindModel(controllerContext, bindingContext);
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者您可以替代地覆盖该CreateModel方法:
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, System.Type modelType)
{
if (modelType == typeof(Project))
{
Project model = new Project()
{
ProjectGuid = System.Guid.NewGuid(),
AccountName = controllerContext.HttpContext.User.Identity.Name,
// don't set name, thats the default binder's job
TotalTime = 0,
};
return model;
}
throw new NotSupportedException("You can only use the ProjectModelBinder on parameters of type Project.");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12480 次 |
| 最近记录: |