I am sure I am missing something here, I found this question to validate a file, here is the sample code
我确信我在这里遗漏了一些东西,我发现这个问题来验证文件,这里是示例代码
public class UpdateSomethingViewModel
{
[DisplayName("evidence")]
[Required(ErrorMessage="You must provide evidence")]
[RegularExpression(@"^abc123.jpg$", ErrorMessage="Stuff and nonsense")]
public HttpPostedFileBase Evidence { get; set; }
}
but I don't see any @Html.FileFor(model => model.Evidence)
但我没有看到任何@ Html.FileFor(model => model.Evidence)
Any ideas?
有任何想法吗?
Update
I found a simple solution passing attribute type in html attribute collection.
我发现了一个在html属性集合中传递属性类型的简单解决方案。
@Html.TextBoxFor(model => model.Evidence, new { type = "file" })
@Html.ValidationMessageFor(model => model.Evidence)
2 个解决方案
#1
13
I found a simple solution passing attribute type
in html attribute collection.
我发现了一个在html属性集合中传递属性类型的简单解决方案。
@Html.TextBoxFor(model => model.Evidence, new { type = "file" })
@Html.ValidationMessageFor(model => model.Evidence)
#2
3
I am afraid you can't do this using data annotations. You could do this in the controller action that is supposed to handle the request:
我担心你不能使用数据注释来做到这一点。您可以在应该处理请求的控制器操作中执行此操作:
Model:
模型:
public class UpdateSomethingViewModel
{
[DisplayName("evidence")]
[Required(ErrorMessage = "You must provide evidence")]
public HttpPostedFileBase Evidence { get; set; }
}
Action:
行动:
[HttpPost]
public ActionResult Foo(UpdateSomethingViewModel model)
{
if (model.Evidence != null && model.Evidence.ContentLength > 0)
{
// the user uploaded a file => validate the name stored
// in model.Evidence.FileName using your regex and if invalid return a
// model state error
if (!Regex.IsMatch(model.Evidence.FileName, @"^abc123.jpg$"))
{
ModelState.AddModelError("Evidence", "Stuff and nonsense");
}
}
...
}
Also note that it is better to use HttpPostedFileBase
rather than the concrete HttpPostedFileWrapper
type in your model. It will make your life easier when you are writing unit tests for this controller action.
另请注意,最好在模型中使用HttpPostedFileBase而不是具体的HttpPostedFileWrapper类型。当您为此控制器操作编写单元测试时,它将使您的生活更轻松。
#1
13
I found a simple solution passing attribute type
in html attribute collection.
我发现了一个在html属性集合中传递属性类型的简单解决方案。
@Html.TextBoxFor(model => model.Evidence, new { type = "file" })
@Html.ValidationMessageFor(model => model.Evidence)
#2
3
I am afraid you can't do this using data annotations. You could do this in the controller action that is supposed to handle the request:
我担心你不能使用数据注释来做到这一点。您可以在应该处理请求的控制器操作中执行此操作:
Model:
模型:
public class UpdateSomethingViewModel
{
[DisplayName("evidence")]
[Required(ErrorMessage = "You must provide evidence")]
public HttpPostedFileBase Evidence { get; set; }
}
Action:
行动:
[HttpPost]
public ActionResult Foo(UpdateSomethingViewModel model)
{
if (model.Evidence != null && model.Evidence.ContentLength > 0)
{
// the user uploaded a file => validate the name stored
// in model.Evidence.FileName using your regex and if invalid return a
// model state error
if (!Regex.IsMatch(model.Evidence.FileName, @"^abc123.jpg$"))
{
ModelState.AddModelError("Evidence", "Stuff and nonsense");
}
}
...
}
Also note that it is better to use HttpPostedFileBase
rather than the concrete HttpPostedFileWrapper
type in your model. It will make your life easier when you are writing unit tests for this controller action.
另请注意,最好在模型中使用HttpPostedFileBase而不是具体的HttpPostedFileWrapper类型。当您为此控制器操作编写单元测试时,它将使您的生活更轻松。