Note that there are some explanatory texts on larger screens.

plurals
  1. PORender HTML tags inside of HTML.ValidationMessageFor in MVC3
    primarykey
    data
    text
    <p>I'm trying to show a link as part of the validation message for a field. I'm using data attributes with custom error messages to set it:</p> <pre><code>[Required(ErrorMessage = "Message &lt;a href='#'&gt;link&lt;/a&gt;")] public string Field{ get; set; } </code></pre> <p>But when it renders the tags are escaped and literally prints:</p> <pre><code>Message &lt;a href='#'&gt;link&lt;/a&gt; </code></pre> <p>Is it possible to have the link as part of the validation message but render correctly?</p> <p><strong>In case anyone's interested, here's how I accomplished it</strong><br /></p> <pre><code>public static MvcHtmlString ValidationHTMLMessageFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; helper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression) { return ValidationHTMLMessageFor(helper, expression, (object)null); } public static MvcHtmlString ValidationHTMLMessageFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; helper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression, object htmlAttributes) { return ValidationHTMLMessageFor(helper, expression, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString ValidationHTMLMessageFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; helper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression, IDictionary&lt;string, object&gt; htmlAttributes) { 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; } string errors = ""; foreach (ModelError error in helper.ViewData.ModelState[name].Errors) { TagBuilder tag = new TagBuilder("span"); tag.Attributes.Add("class", HtmlHelper.ValidationMessageCssClassName); tag.MergeAttributes(htmlAttributes); tag.Attributes.Add("data-valmsg-for", name); tag.Attributes.Add("data-valmsg-replace", "true"); var text = tag.ToString(TagRenderMode.StartTag); text += error.ErrorMessage; text += tag.ToString(TagRenderMode.EndTag); errors += text; } return MvcHtmlString.Create(errors); } </code></pre> <p>Thanks Darin for pointing me in the right direction. I also found this that I used as a template <a href="https://stackoverflow.com/questions/8004105/customize-html-validationmessagefor-doesnt-work-in-client-side">Customize Html.ValidationMessageFor doesn&#39;t work in client side</a>.</p> <p>I'm new to this, so if anyone has any suggestions, please post. Thanks!</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