Rita
Rita

Reputation: 1237

ASP.NET MVC Page - hyper links in HTML.ValidationSummary

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

Now i have to display the Hyperlink in that Validation Error Message. But it is treating href also as string.

The Validation Message that I am trying to display with hyperlink is: **"User already exists in the system, please <a href='../Login.aspx'>login</a>"**

Appreciate your responses.

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>
<% } %>

Upvotes: 2

Views: 3626

Answers (2)

epignosisx
epignosisx

Reputation: 6192

The server side solution is better, but in the client side we could run a quick trick:

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

Upvotes: 4

Nathan Taylor
Nathan Taylor

Reputation: 24606

The default ValidationSummary helper returns an HTML encoded value; if you would like to allow HTML inside of the ValidationSummary you will need to create your own version of it. Take a look at the source code in the MVC 2 RTM source code to get started. The ValidationSummary HtmlHelper is defined in the System.Web.Mvc.Html.ValidationExtensions class (located in mvc2-rtm-sources\src\SystemWebMvc\Mvc\Html).

Taking a look at the source code in ValidationExtensions.cs it would appear 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 produce the behavior you desire; however, it won't be quite that simple because the ValidationSummary method calls some internal System.Web.Mvc methods that you're going to have to manually implement in your customized version in order to reproduce the original behavior.

Upvotes: 3

Related Questions