is there any way to validate using ModelState and model, to determine the type of error?
example:
[Required]
I wanted to make a custom ValidationMessageFor, and within it whether the error is [Required] or other type of error.
I am using DataAnnotations and CodeFirst.
Have is code from start implemetantion:
public static MvcHtmlString MyValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
{
var Validacao = "";
if (helper.ViewData.ModelState[((MemberExpression)expression.Body).Member.Name] != null)
{
foreach (var erros in helper.ViewData.ModelState[((MemberExpression)expression.Body).Member.Name].Errors)
{
Validacao = Validacao + " *";
}
}
return MvcHtmlString.Create(Validacao);
}
tks
Any validation attribute has ErrorMessage property you can use to specify error message directly in attribute. Example: