C# asp.net MVC 2使用数据批注验证文件上载
我正在尝试使用数据注释验证表单。这对于字符串类型和整数似乎很好,但是对于文件上传,我无法从类中验证。它将被发送一个字符串HttpPostedFileWrapper。有人有什么建议吗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"
谢谢您可以根据一般用法使用数据注释 例如,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正是我一直在寻找的。