MVC视图中的ValidationMessage不显示

时间:2017-01-13 15:08:57

标签: asp.net-mvc validation

我有自定义日期验证,我按照说明完成了   this链接。下面是我的模型代码:

    [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; }

` 以下是我在View中的代码:

<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" })

以下是我在Controller中的代码:

 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");

即使我试图显示模型类中提到的验证消息,我只获得星形图像而不是验证消息。但是,将显示验证摘要中指定的错误消息。但我想在模型类中显示消息。任何线索?

2 个答案:

答案 0 :(得分:0)

我在View中的ValidationMessageFor上删除了星号。它开始工作..可能对某人有帮助。

答案 1 :(得分:0)

您的属性的ValidationResult没有与字段关联的密钥,这会阻止ValidationMessageFor工作。

我建议在ViewModel上使用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) });
        }
    }