我的数据模型中有以下属性:
[Required]
[DataType(DataType.Text)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
我的文本框当前有一个占位符,这样当他们专注于文本框时,占位符将在文本框中消失,如果他们没有输入任何内容,那么文本框val($(textbox).val()
)是如果FirstName
或{{1等于“名字”和“姓氏”
答案 0 :(得分:6)
您应该编写自己的ValidationAttribute并在属性上使用它
简单示例:
public sealed class PlaceHolderAttribute:ValidationAttribute
{
private readonly string _placeholderValue;
public override bool IsValid(object value)
{
var stringValue = value.ToString();
if (stringValue == _placeholderValue)
{
ErrorMessage = string.Format("Please fill out {0}", _placeholderValue);
return false;
}
return true;
}
public PlaceHolderAttribute(string placeholderValue)
{
_placeholderValue = placeholderValue;
}
}
在您的财产上使用它:
[Required]
[DataType(DataType.Text)]
[Display(Name = "First Name")]
[PlaceHolder("First Name")]
public string FirstName { get; set; }