Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
969 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...