Link in validation summary message

后端 未结 6 1464
野性不改
野性不改 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: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 ...

提交回复
热议问题