Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 重定向到mvc.net中应用程序错误的同一页_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 重定向到mvc.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

我在MVC2.net工作,在文件上传方面遇到了问题。如果文件大小超过表单限制,则我希望在同一页面上显示一些异常。

过去,我创建了一个图像大小属性,并使用数据注释验证图像

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);