【问题标题】:Inconsistent behavior with Html.ValidationMessageFor() method与 Html.ValidationMessageFor() 方法不一致的行为
【发布时间】:2012-03-04 10:58:34
【问题描述】:

我正在使用 Html.ValidationMessageFor() 方法在表单上显示验证消息。对于大多数表单字段,除了几个字段外,我都会收到相应的错误消息。一个是“出生日期”字段,它是表单上的一个文本框,另一个是“州”字段,它是美国所有州的下拉列表。对于这两个字段,我得到“DateOfBirth 字段是必需的”。和 'State 字段是必需的。'

我启动了反射器并查看了 Html.ValidationMessageFor() 静态方法中的代码,这些方法似乎没有添加此消息。

有什么想法可能来自此消息以及如何解决它?

更新 添加了模型代码...基本上我使用的是精简版的 CSLA。业务规则在创建时添加到类中,并触发一次,调用适当的方法。基本上,'rule.ErrorMessage' 是 ValidationMessageFor 列出错误时应该显示的内容。它适用于所有其他属性,但 DateofBirth 和 State 属性除外。

型号代码

属性:

public Guid UserId { get; set; }
public Guid PatientId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone1 { get; set; }
public string Phone2 { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public States State { get; set; }
public string Zip { get; set; }
public DateTime DateOfBirth { get; set; }
public string EmergencyName { get; set; }
public string EmergencyPhone { get; set; }
public string EmergencyRelation { get; set; }

业务规则:

this.AddValidation(new DataValidation("DateOfBirth", "DateOfBirthRequired",
   delegate(DataValidation rule)
   {
      rule.ErrorMessage = "required";
      return ((DateOfBirth >= new DateTime(1900, 1, 1)) && (DateOfBirth < DateTime.Today));
   }, DataAction.Update | DataAction.Insert));
this.AddValidation(new DataValidation("State", "StateRequired",
   delegate(DataValidation rule)
   {
      rule.ErrorMessage = "required";
      return (State != States.Unknown);
   }, DataAction.Insert | DataAction.Update));

【问题讨论】:

  • 你的模型是什么样的?你能提供它的源代码吗?
  • 从模型中添加了一些源代码。

标签: asp.net-mvc asp.net-mvc-3


【解决方案1】:

我需要做的就是将属性设置为可空,如this post 中所述。

【讨论】:

    猜你喜欢
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 2010-12-23
    • 2013-09-02
    • 2020-02-28
    • 2022-01-24
    相关资源
    最近更新 更多