我编写自己的属性来验证ASP.NET MVC中的模型:
public class ValidateImage : RequiredAttribute, IClientValidatable
{
public override bool IsValid(object value)
{
// validate object
}
}我是这样使用这些属性的:
public class MyModel
{
[ValidateImage]
public HttpPostedFileBase file { get; set; }
}现在,我想让它在控制器中工作,我将这个属性添加到一个属性中,而不是模型中:
public ActionResult EmployeePhoto(string id, [ValidateImage] HttpPostedFileBase file)
{
if(ModelState.IsValid)
{
}
}但是我的属性永远不会执行。如何在不使用模型的情况下在控制器中进行验证?
发布于 2013-05-28 15:24:21
这不受支持。只需编写一个视图模型来包装操作的所有参数:
public ActionResult EmployeePhoto(EmployeePhotoViewModel model)
{
if (ModelState.IsValid)
{
}
}它可能看起来像这样:
public class EmployeePhotoViewModel
{
public string Id { get; set; }
[ValidateImage]
public HttpPostedFileBase File { get; set; }
}https://stackoverflow.com/questions/16786117
复制相似问题