views:

310

answers:

2

Hi

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>
<% } %>
+3  A: 

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.

Nathan Taylor
Yup......... It is not that simple....
Rita
What do you mean?
Nathan Taylor
As you mentioned........ looks like it won't be quite that simple.
Rita
Yeah, I see what you mean. I just looked at it a bit and there are quite a few internal/private calls. If you don't want to screw with their code too much I suppose you could make a helper that wraps the ValidationSummary's output and decodes the values back into Html- but that doesn't sound very ideal.
Nathan Taylor
A: 

Would dwilliams469's post help you?

http://forums.asp.net/t/1377525.aspx

BMD86