我有一個自定義日期驗證和在 this鏈接解釋我做了。下面是我的型號代碼:ValidationMessage在MVC視圖不顯示
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",ApplyFormatInEditMode=true)]
[DisplayName("Sch.Start Date:")]
[DataType(DataType.Date)]
[ValidProjectDate(ErrorMessage="Project Start Date cannot be greater than Project End Date.")]
public DateTime? ProjectScheduleStartDate { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[DisplayName("Sch.End Date:")]
[DataType(DataType.Date)]
[ValidProjectDate(ErrorMessage = "Project End Date cannot be less than or Equal to Current Date.")]
public DateTime? ProjectScheduleEndDate { get; set; }
` 下面是我在查看代碼:
<div class="form-group">
@Html.LabelFor(model => model.ProjectScheduleStartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectScheduleStartDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectScheduleStartDate, "*", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectScheduleEndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectScheduleEndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectScheduleEndDate, "*", new { @class = "text-danger" })
</div>
</div>
<hr/>
@Html.ValidationSummary(true, "Please correct below errors", new { @class = "text-danger" })
下面是我在控制器代碼:
if (ModelState.IsValid)
{
ProjectManager pm = new ProjectManager();
pm.AddProjects(prj);
ViewBag.Result = "Record Inserted Successfully.";
ModelState.Clear();
}
else
{
ModelState.AddModelError(string.Empty, "An Error has happened");
}
return View("AddNewProject");
即使盡管我試圖顯示驗證消息如模型類中提到的,我只獲取星形圖像而不是驗證消息。但是,在驗證摘要中指定的錯誤消息正在顯示。但我想在模型類中顯示消息。任何線索?