How to customize Html.ValidationMessageFor in ASP MVC

后端 未结 7 864
Happy的楠姐
Happy的楠姐 2020-12-05 07:46

Is it possible to customize the Html.ValidationMessageFor method so that it produces different HTML?

I want to do something similar to:

7条回答
  •  借酒劲吻你
    2020-12-05 08:02

    Maybe you can put that code

    string propertyName = ExpressionHelper.GetExpressionText(expression);
    string name = helper.AttributeEncode(helper.ViewData.TemplateInfo.GetFullHtmlFieldName(propertyName));
    
    if (helper.ViewData.ModelState[name] == null ||
        helper.ViewData.ModelState[name].Errors == null ||
        helper.ViewData.ModelState[name].Errors.Count == 0)
        {
            return MvcHtmlString.Empty;
        }
    

    on top of the answered function, so that the div doesn't appear on the form load.

提交回复
热议问题