Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link in validation summary message

Is it possible to put a HTML link in validation summary message? For example I want to put a link to another page in case there is validation error:

@Html.ValidationSummary(False, "read <a href=""anotherpage.html"">more</a>")

or

@Html.ValidationSummary(False, "read " &
    Html.ActionLink("more", "helpforerror").ToHtmlString)

But in the browser the tag is escaped so it doesn't form a link.

like image 557
Endy Tjahjono Avatar asked Jun 06 '12 03:06

Endy Tjahjono


People also ask

How do you show custom validator error message in validation summary?

In order for the Validator 's error message to display in the ValidationSummary , you need to set the Validator s Display="none" . I also set Text="" . C.C. C.C.

What is validation summary?

The ValidationSummary class is used to summarize the error messages from all validators on a Web page in a single location. You can summarize the error messages from a group of validators on a Web page by assigning the ValidationSummary control to a validation group by setting the ValidationGroup property.

What is HTML validation summary?

The ValidationSummary() extension method displays a summary of all validation errors on a web page as an unordered list element. It can also be used to display custom error messages.

What is validation summary control in asp net?

The ValidationSummary control enables you to display a list of all the validation errors in a page in one location. This control is particularly useful when working with large forms. If a user enters the wrong value for a form field located toward the end of the page, then the user might never see the error message.


2 Answers

I know you have accepted an answer, but i think my solution is more simple and will require less rewriting if you want to add links to existing validation summaries.

You need to put a {0} type format item in your validation message like below, which will be replaced by your link.

ModelState.AddModelError("", "Some error message with a link here {0}.");

then in your view call your validation summary like so:

@string.Format(Html.ValidationSummary().ToString(), Html.ActionLink("Click Here", "Action_To_Link_To")).ToHtmlString()

In this case i have used an extension method I added to the string object .ToHtmlString() that basically just converts the string to an HtmlString preventing any of the markup being escaped. it looks like this:

public static HtmlString ToHtmlString(this String str)
{
    return new HtmlString(str);
}
like image 132
Ben Avatar answered Jan 02 '23 16:01

Ben


Finally I chose another way to do it: create a div containing the link etc. outside of validation summary, and add the div only if modelstate is not valid:

@If Not ViewData.ModelState.IsValid Then
    @<div>read <a href="anotherpage.html">more</a></div>
End If

This is inspired by an answer to similar question.

like image 37
Endy Tjahjono Avatar answered Jan 02 '23 15:01

Endy Tjahjono