Ich habe eine benutzerdefinierte Datum Validierung und ich habe getan, wie in this Link erläutert. Unten ist mein Modell Code:ValidationMessage in MVC Ansicht nicht angezeigt
[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; }
` Unten ist mein Code in Aussicht:
<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" })
Unter meinen Code in Controller ist:
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");
Selbst obwohl ich versucht habe, die Validierungsnachricht anzuzeigen Wie in der Modellklasse erwähnt, erhalte ich nur Sternbilder anstelle der Validierungsnachrichten. Die in der Validierungszusammenfassung angegebenen Fehlermeldungen werden jedoch angezeigt. Aber ich möchte die Nachrichten in der Modellklasse anzeigen. Irgendeine Ahnung?