MVC 3 Display HTML inside a ValidationSummary

前端 未结 5 1558
遥遥无期
遥遥无期 2021-02-14 14:18

I am trying to display a strong tag inside a validation summary but it encodes it and does not display properly.

@Html.ValidationSummary(false, \"E         


        
相关标签:
5条回答
  • 2021-02-14 14:48

    I have a site that uses Resource files for language. In one of the items I placed this for the Value: <img src="images/exclamation.png" > <strong>Pharmacy Name is required</strong> and it works.

    0 讨论(0)
  • 2021-02-14 14:49

    You could extend the ValidationSummary helper as has been suggested in the accepted answer on this question.

    Edit: I presume the encoding of any text entered is a security feature and therefore a good thing.

    0 讨论(0)
  • 2021-02-14 15:01

    The easiest way:

    @if (!ViewData.ModelState.IsValid)
    {
    <div>@Html.Raw(HttpUtility.HtmlDecode(Html.ValidationSummary(false, "<strong>ERROR:<strong>The form is not valid!").ToHtmlString()))</div>
    }
    
    0 讨论(0)
  • 2021-02-14 15:03
    @Html.Raw(System.Web.HttpUtility.HtmlDecode((Html.ValidationSummary(false) ?? (object)"").ToString()))
    
    0 讨论(0)
  • 2021-02-14 15:09

    I've find this :

        public static MvcHtmlString ToMvcHtmlString(this MvcHtmlString htmlString)
        {
            if (htmlString != null)
            {
                return new MvcHtmlString(HttpUtility.HtmlDecode(htmlString.ToString()));
            }
            return null;
        }
    

    and then :

    @Html.ValidationSummary(false, "<strong>ERROR:<strong>The form is not valid!").ToMvcHtmlString()
    
    0 讨论(0)
提交回复
热议问题