Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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
C# asp.net MVC 2使用数据批注验证文件上载_C#_Asp.net Mvc_Data Annotations - Fatal编程技术网

C# asp.net MVC 2使用数据批注验证文件上载

C# asp.net MVC 2使用数据批注验证文件上载,c#,asp.net-mvc,data-annotations,C#,Asp.net Mvc,Data Annotations,我正在尝试使用数据注释验证表单。这对于字符串类型和整数似乎很好,但是对于文件上传,我无法从类中验证。它将被发送一个字符串HttpPostedFileWrapper。有人有什么建议吗 谢谢您可以根据一般用法使用数据注释 例如,viewmodel,例如: public class UpdateSomethingViewModel { [DisplayName("evidence")] [Required(ErrorMessage="You must provide evidence"

我正在尝试使用数据注释验证表单。这对于字符串类型和整数似乎很好,但是对于文件上传,我无法从类中验证。它将被发送一个字符串HttpPostedFileWrapper。有人有什么建议吗


谢谢

您可以根据一般用法使用数据注释

例如,viewmodel,例如:

public class UpdateSomethingViewModel {
    [DisplayName("evidence")]
    [Required(ErrorMessage="You must provide evidence")]
    [RegularExpression(@"^abc123.jpg$", ErrorMessage="Stuff and nonsense")]
    public HttpPostedFileWrapper Evidence { get; set; }
}
然后,在您的控制器中,与往常一样:

[HttpPost]
public ActionResult UpdateSomething(UpdateHSomethingViewModel model)
{
    if (ModelState.IsValid)
    {
        // do stuff - plenty of stuff
        // weee, we're off to see the wizard.

        return RedirectToAction("UpdateSomethingSuccess", model);
    }

    return View(model);
}
我刚刚在MVC2/.NET4中进行了测试,效果不错

希望有帮助

干杯,
Terry

您可以根据一般用法使用数据注释

例如,viewmodel,例如:

public class UpdateSomethingViewModel {
    [DisplayName("evidence")]
    [Required(ErrorMessage="You must provide evidence")]
    [RegularExpression(@"^abc123.jpg$", ErrorMessage="Stuff and nonsense")]
    public HttpPostedFileWrapper Evidence { get; set; }
}
然后,在您的控制器中,与往常一样:

[HttpPost]
public ActionResult UpdateSomething(UpdateHSomethingViewModel model)
{
    if (ModelState.IsValid)
    {
        // do stuff - plenty of stuff
        // weee, we're off to see the wizard.

        return RedirectToAction("UpdateSomethingSuccess", model);
    }

    return View(model);
}
我刚刚在MVC2/.NET4中进行了测试,效果不错

希望有帮助

干杯,
Terry

有没有可能提供一些示例代码?有没有可能提供一些示例代码?HttpPostedFileWrapper正是我一直在寻找的。HttpPostedFileWrapper正是我一直在寻找的。