2010-03-09 66 views
1

我想验证一个窗体使用数据注释。对字符串类型和整数看起来很不错,但对于文件上传,我无法从类中进行验证。它只会发送一个字符串“HttpPostedFileWrapper”。有没有人有任何提示?asp.net MVC 2验证文件上传与数据注释

谢谢

+0

任何机会,你可以提供一些示例代码? – belugabob

回答

4

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

例如,视图模型如:

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/.NET 4)和它工作过一种享受。

希望有所帮助。

干杯, 特里

+0

HttpPostedFileWrapper就是我一直在寻找的东西。 –