ASP.NET MVC Page - Hyperlinks in HTML.ValidationSummary

I have a registration page and if the validation fails, it displays error messages using the HTML.ValidationSummary control.

Now I need to display a hyperlink in this validation error message. But it treats the href just like a string.

The validation message I'm trying to display using a hyperlink: **"User already exists in the system, please <a href='../Login.aspx'>login</a>"**

Rate your answers.

Here is my code:

<% using (Html.BeginForm()) {%>
    <%= Html.ValidationSummary(false) %>
    <fieldset>
    <div class="cssform">;
        <p>
            <%= Html.LabelFor(model => model.email)%><em>*</em>
            <%= Html.TextBoxFor(model => model.email, new { @class = "required email" })%>
            <%= Html.ValidationMessageFor(model => model.email)%>
        </p>
        <p>
            <%= Html.Label("Confirm email")%><em>*</em>
            <%= Html.TextBox("confirm_email")%>
            <%= Html.ValidationMessage("confirm_email") %>
        </p>
        <p>
            <%= Html.Label("Password")%><em>*</em>
            <%= Html.Password("Password", null, new { @class = "required" })%>
            <%= Html.ValidationMessage("Password")%><br />
            (Note: Password should be minimum 6 characters)
        </p>
        <p>
            <%= Html.Label("Confirm Password")%><em>*</em>
            <%= Html.Password("confirm_password")%>
            <%= Html.ValidationMessage("confirm_password") %>
        </p><hr />
    </div>
    <p><input type="submit" value="Register" /></p>
    </fieldset>
<% } %>

      

+2


a source to share


2 answers


The ValidationSummary helper returns an HTML-encoded value by default; if you want to allow HTML inside ValidationSummary you will need to create your own version. To get started, check out the source code for RTM MVC 2 source code . ValidationSummary HtmlHelper is defined in the System.Web.Mvc.Html.ValidationExtensions class (found in mvc2-rtm-sources \ src \ SystemWebMvc \ Mvc \ Html).

Taking a look at the source code at ValidationExtensions.cs, it seems that listItem.SetInnerText(errorText)

is the culprit:



if (modelStates != null) {
    foreach (ModelState modelState in modelStates) {
        foreach (ModelError modelError in modelState.Errors) {
            string errorText = GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError, null /* modelState */);
            if (!String.IsNullOrEmpty(errorText)) {
                TagBuilder listItem = new TagBuilder("li");
                listItem.SetInnerText(errorText);
                htmlSummary.AppendLine(listItem.ToString(TagRenderMode.Normal));
            }
        }
    }
}

      

Changing the code to listItem.InnerHtml = errorText

will result in the desired behavior; however, this will not be so easy because the ValidationSummary method calls some of the internal methods of System.Web.Mvc, which you will have to manually implement in your custom version to reproduce the original behavior.

+3


a source


The server side solution is better, but on the client side we can do a quick trick:



$(function(){
    $(".validation-summary-errors").find("li").each(function () {
        var $this = $(this);
        $this.html($this.text());
    });
});

      

+2


a source







All Articles