Asp.net mvc 重定向到mvc.net中应用程序错误的同一页
我在MVC2.net工作,在文件上传方面遇到了问题。如果文件大小超过表单限制,则我希望在同一页面上显示一些异常。过去,我创建了一个图像大小属性,并使用数据注释验证图像Asp.net mvc 重定向到mvc.net中应用程序错误的同一页,asp.net-mvc,Asp.net Mvc,我在MVC2.net工作,在文件上传方面遇到了问题。如果文件大小超过表单限制,则我希望在同一页面上显示一些异常。过去,我创建了一个图像大小属性,并使用数据注释验证图像 public sealed class ImageSizeAttribute : ValidationAttribute { public int Width { get; set; } public int Height { get; set; } private const string Defaul
public sealed class ImageSizeAttribute : ValidationAttribute
{
public int Width { get; set; }
public int Height { get; set; }
private const string DefaultErrorMessage = "{0} dimensions cannot be greater than {1} x {2}";
public ImageSizeAttribute(int width, int height)
: base(DefaultErrorMessage)
{
Width = width;
Height = height;
}
public override string FormatErrorMessage(string name)
{
return string.Format(CultureInfo.CurrentUICulture, ErrorMessageString, name, Width, Height);
}
public override bool IsValid(object value)
{
// Turn HttpPostedFileBase into Image and validate size...
}
}
在视图模型中,现在只需添加一个属性即可对其进行验证
[ImageSize(200, 200)]
public HttpPostedFileBase Avatar { get; set; }
在您的视图中,确保至少有一条验证消息
<%= Html.ValidationMessageFor(u => u.Avatar) %>
嗨,也许这篇文章能帮上忙
if (ModelState.IsValid)
{
// More validation and saving.
...
return RedirectToRoute("UserDetails", ...);
}
return View(model);