Link in validation summary message

后端 未结 6 1465
野性不改
野性不改 2020-12-20 17:33

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.Va         


        
相关标签:
6条回答
  • 2020-12-20 17:39

    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.

    0 讨论(0)
  • 2020-12-20 17:40

    If you're sending back HTML in the ModelStateError, you can use this one liner:

    @Html.Raw(HttpUtility.HtmlDecode(Html.ValidationSummary().ToHtmlString()))
    

    It's very similar to what @Benoit had suggested, just without needing the extension.

    0 讨论(0)
  • 2020-12-20 17:42

    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);
    }
    
    0 讨论(0)
  • 2020-12-20 17:50

    The validation text is encoded before the ValidationSumary or ValidationFor, etc...

    you just need tu decode the html, then create an MvcHtmlString ...

    Exemple :

    @HttpUtility.HtmlDecode(Html.ValidationSummary().ToString()).ToMvcHtmlString()
    

    this is an extension i have made to make MvcHtmlString :

    namespace System
    {
        public static class StringExtension
        {
            public static System.Web.Mvc.MvcHtmlString ToMvcHtmlString(this string value)
            {
            return System.Web.Mvc.MvcHtmlString.Create(value);
            }
        }
     }
    

    or you can create an HtmlHelper if you plan to reuse this:

    namespace System.Web.Mvc.Html
    {
        public static class FormHelper
        {
            public static MvcHtmlString ValidationSummaryEx(this HtmlHelper htmlHelper, bool excludePropertyErrors)
            {
                var original = htmlHelper.ValidationSummary(excludePropertyErrors);
                var decoded = HttpUtility.HtmlDecode(original.ToString());
                return decoded.ToMvcHtmlString();
            }
        }
    }
    

    Hope it help you or future viewer. Note: it work for all validations Summary and ValidationFor ...

    0 讨论(0)
  • 2020-12-20 17:53

    You can check if form is valid by jquery and update div with link text:

    <div id="divToUpdate">
    
    </div>
    
    $('form').submit(function(){
       if(!this.valid())
           $('#divToUpdate').html("read <a href='anotherpage.html'>more</a>"); 
    });
    
    0 讨论(0)
  • 2020-12-20 17:55

    No, the default behaviour doesn't allow it, but you can make your own. This is what you need: Html raw in validationsummary

    0 讨论(0)
提交回复
热议问题