2017-01-13 61 views
0

我有一個自定義日期驗證和在 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"); 

即使盡管我試圖顯示驗證消息如模型類中提到的,我只獲取星形圖像而不是驗證消息。但是,在驗證摘要中指定的錯誤消息正在顯示。但我想在模型類中顯示消息。任何線索?

回答

0

我刪除的ValidationMessageFor在查看星號。它開始工作..可能對某人有幫助。

0

你的屬性的的ValidationResult沒有一個領域,防止ValidationMessageFor從工作相關的密鑰。

我會建議使用實現您的視圖模型,而不是您的自定義數據驗證屬性IValidatable接口,除非有你的應用中有許多地方需要這種類型的驗證。

你會發現,它提供的屬性作爲鍵的名稱與該領域的錯誤關聯:

public class TestViewModel : IValidatableObject 
    { 
     public DateTime? ProjectStartDate { get; set; } 
     public DateTime? ProjectEndDate { get; set; } 

     public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
     { 
      if (ProjectStartDate > ProjectEndDate) 
       yield return new ValidationResult("The Project Start Date cannot be after the Project End Date.", new List<string>() { nameof(ProjectStartDate) }); 
      if (ProjectEndDate < DateTime.Now) 
       yield return new ValidationResult("Project End Date cannot be less than or Equal to Current Date.", new List<string>() { nameof(ProjectEndDate) }); 
     } 
    }