我的ApplicationController
:
public class ApplicationController : Controller
{
private IUxRepository _repository;
public ApplicationController(IUxRepository repository)
{
_repository = repository;
}
public ActionResult Create()
{
return View("Shape", new ApplicationViewModel());
}
}
我的ApplicationViewModel
public class ApplicationViewModel : ViewModelBase
{
public ApplicationViewModel()
{
Application = new Application();
}
public Application Application {get;set;}
}
我的Application
型号:
public class Application : DbEntity
{
public string Name {get;set;}
[Display(Name = "Proposed Release Date"),
RegularExpression(@"(^Q[1-4])\s(2\d{3})", ErrorMessage = "Date needs to be in the format Q{1-4}{space}20{YY} e.g. Q4 2013 or Q1 2014")]
public string ProposedReleaseDate {get;set;}
}
摘自Shape
视图:
<div class="editor-label">
@Html.DisplayFor(model => model.ProposedReleaseDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProposedReleaseDate)
@Html.ValidationMessageFor(model => model.ProposedReleaseDate)
</div>
当我尝试加载Shape
视图时,出于某种原因,它会返回验证错误,指出ProposedReleaseDate
不符合所需的RegularExpression
。
当然不是因为它是一个等待输入的新实体,为什么在页面加载之前进行验证。它的验证太早了。如何绕过这个,或者我在哪里关闭此功能,它的反直觉/高效恕我直言。
答案 0 :(得分:1)
ErrorMessage格式不正确。而不是
ErrorMessage = "Date needs to be in the format Q{1-4}{space}20{YY} e.g. Q4 2013 or Q1 2014"
制作
ErrorMessage = "Date needs to be in the format Q{{1-4}}{{space}}20{{YY}} e.g. Q4 2013 or Q1 2014"
请注意,“{”现在是“{{”和“}”现在是“}}”
修改:相关链接How to escape braces (curly brackets) in a format string in .NET