更改

时间:2017-09-03 09:32:36

标签: jquery asp.net-mvc html5 validation

我使用数据注释来验证我的模型:

...

    [Required(ErrorMessage = "Please enter a due date of the task")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
    [DisplayName("Due Date")]
    public DateTime? DueDate { get; set; }

...

    <div class="form-group">
        @Html.LabelFor(model => model.DueDate, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-3">
            @Html.EditorFor(model => model.DueDate, new { htmlAttributes = new { @class = "form-control", onchange = "handler(event);" } })
            @Html.ValidationMessageFor(model => model.DueDate, "", new { @class = "text-danger" })
        </div>
    </div>

如果选择了正确的日期,我需要隐藏错误消息,但只有在提交表单时才会验证。

just submitted the form

enter image description here

有没有人知道为什么会这样?

UPD:控制器方法

public PartialViewResult Edit(Guid? id)
    {
        TaskViewModel tvm = new TaskViewModel();

        // if it is not new task
        if (id != null)
        {
            // retrieve the task from the store
            Task task = _repository.GetTask(id.Value);

            // fill metadata if the task was found
            if (task != null)
            {
                tvm.TaskId = task.TaskId;
                tvm.Title = task.Title;
                tvm.Notes = task.Notes;
                tvm.Priority = task.Priority;
                tvm.IterationPath = task.IterationPath;
                tvm.DueDate = task.DueDate;
                tvm.AssigneePerson = task.AssigneePerson;
            }
        }

        return PartialView("EditTaskView", tvm);
    }

0 个答案:

没有答案