Note that there are some explanatory texts on larger screens.

plurals
  1. POCustomize Html.ValidationMessageFor doesn't work in client side
    primarykey
    data
    text
    <p>I am working on mvc3 application, i have created a custom validationMessage to validate a simple field. This custom validationMessage only show an div with title and message instead of the default span, i have this :</p> <pre><code>@Html.ValidationMessageCustomFor(model =&gt; model.FirstName) </code></pre> <p>This is the code to my custom validation:</p> <pre><code>public static MvcHtmlString ValidationCustomFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; helper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression) { 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; } StringBuilder htmlValidation = new StringBuilder(); htmlValidation.AppendLine("&lt;div&gt;"); htmlValidation.AppendLine("&lt;table&gt;"); htmlValidation.AppendLine("&lt;tr&gt;"); htmlValidation.AppendLine("&lt;td&gt;&lt;img src='/Content/Imgages/Error.jpg'&gt;&lt;/td&gt;"); htmlValidation.AppendLine("&lt;/tr&gt;"); htmlValidation.AppendLine("&lt;tr&gt;"); htmlValidation.AppendFormat("&lt;td&gt;{0}&lt;/td&gt;", name); htmlValidation.AppendLine("&lt;/tr&gt;"); htmlValidation.AppendLine("&lt;tr&gt;"); htmlValidation.AppendFormat("&lt;td&gt;{0}&lt;/td&gt;", helper.ViewData.ModelState[name].Errors[0].ErrorMessage); htmlValidation.AppendLine("&lt;/tr&gt;"); htmlValidation.AppendLine("&lt;/table&gt;"); htmlValidation.AppendLine("&lt;/div&gt;"); return MvcHtmlString.Create(htmlValidation.ToString()); } </code></pre> <p>This works fine when I use server side validation, but when I active client side validation it doesn't work.</p> <p>The default Html.ValidationMessageFor yes works fine with client side validation, but my custom Html.ValidationMessageCustomFor doesn't work.</p> <p>Could you please help me, any ideas?</p> <hr> <p>I modified my custom helper, I added the "data-valmsg-for" atrib for the span, it is the code :</p> <pre><code> public static MvcHtmlString ValidationCustomFor2&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; helper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression) { 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; } TagBuilder tag = new TagBuilder("span"); tag.Attributes.Add("class", "field-validation-error"); tag.Attributes.Add("data-valmsg-for", name); tag.Attributes.Add("data-valmsg-replace", "true"); var text = tag.ToString(TagRenderMode.StartTag); text += "&lt;b&gt;My custom html&lt;/b&gt;"; text += tag.ToString(TagRenderMode.EndTag); return MvcHtmlString.Create(text); } </code></pre> <p>But it doesn't appear, it looks I should do something from the client side, but I don't know WHAT? Any ideas please?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload