How can I render html in validation message in ASP.NET MVC?

后端 未结 3 2059
醉梦人生
醉梦人生 2020-12-13 19:42

I am currently developing a registration page. When user already exists I want to provide login and reset password links for user in error message for email field. In contro

相关标签:
3条回答
  • 2020-12-13 19:58
    @Html.Raw(HttpUtility.HtmlDecode(Html.ValidationMessageFor(m => m.Email).ToHtmlString()))
    

    Isn't pretty though

    0 讨论(0)
  • 2020-12-13 20:09

    Read this post for evaluate errors

    How to add validation errors in the validation collection asp.net mvc?

    In your view

    Html.ValidationMessage("Email")

    0 讨论(0)
  • 2020-12-13 20:12

    Found this post while figuring this out myself using ASP.NET Core.

    What I ended up doing was adding my part of my validation message in modelstate.AddError(), and separately adding to ViewData the bit that had the Html I wanted to render, like so:

    ViewData["myKey"]= "My html";

    It feels pretty ugly and there's probably better ways of doing it, but for my very limited needs, this fit the bill nicely.

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