0

我正在尝试对 MVC razor 进行 jquery 验证并正常工作。我以前在 MVC2 中做过这个,但我需要弄清楚如何使用 LabelFor(DateOfBirth => Model.DateOfBirth) 来让它工作......

由于某种原因,该消息永远不会在标签中弹出。我看过调试器,我也不在那里

<tr><td>
      @Html.LabelFor(DateOfBirth => Model.DateOfBirth)
      </td>
      <td>
      @Html.TextBoxFor(DateOfBirth => Model.DateOfBirth, new { @Value = Model.DateOfBirth })
</td></tr>

那将是包含我的 DateOfBirth 行元素的表行...

rules:{
       DateOfBirth: {
            required: true
       }
},
messages: {
            HospitalFinNumber: 'Please Enter a valid Hospital Fin number',
            DateOfBirth: 'Please enter a valid Date Of Birth',
            AdmitDate: 'Please select an Admit Date',
            Comment: 'Why dont you stop attempting to put in more than 4000 characters?  Thanks...'
}

我的 DateOfBirth 消息没有显示在标签中……我错过了什么吗?

4

2 回答 2

1

您使用 HTML 帮助程序和属性的方式似乎一切都很好。要检查的两件事:

-确保您正在导入 jquery.validate 库

- 确保在您的模型中,您的属性DateOfBirth使用 [ Required ] 属性进行修饰,并设置ErrorMessage属性,如下所示:

    [Required(ErrorMessage="Field Required")]
    public Datetime DateOfBirth { get; set; }
于 2012-08-13T18:57:55.480 回答
0

您的 lambda 格式不正确。

@Html.LabelFor(DateOfBirth => Model.DateOfBirth)

应该

<tr><td>
  @Html.LabelFor(m => m.DateOfBirth)
  </td>
  <td>
  @Html.TextBoxFor(m=> m.DateOfBirth, new { @Value = Model.DateOfBirth })
</td></tr>
于 2012-08-13T18:59:36.070 回答