62,046
社区成员
发帖
与我相关
我的任务
分享
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
public class SelectNotEmptyAttribute : ValidationAttribute, IClientModelValidator
{
public override bool IsValid(object value)
{
if ((value == null) || (value.ToString() == "") || (value.ToString() == "undefined"))
{ return false; }
else
{ return true; }
}
public void AddValidation(ClientModelValidationContext context)
{
MergeAttribute(context.Attributes, "data-val", "true");
var errorMessage = FormatErrorMessage(context.ModelMetadata.GetDisplayName());
MergeAttribute(context.Attributes, "data-val-selectnotempty", errorMessage);
}
private bool MergeAttribute(IDictionary<string, string> attributes,string key, string value)
{
if (attributes.ContainsKey(key))
{ return false; }
attributes.Add(key, value);
return true;
}
}
[Display(Name = "隐患类别"),Required, SelectNotEmpty]
public string Type { get; set; }