C# 如何在 ASP.NET MVC 中的验证消息中呈现 html?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14999130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:43:14  来源:igfitidea点击:

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

c#asp.net-mvcrazor

提问by Dmytro

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 controller I have:

我目前正在开发一个注册页面。当用户已经存在时,我想在电子邮件字段的错误消息中为用户提供登录和重置密码链接。在控制器中,我有:

[HttpPost]
public ActionResult Register(RegistrationModel registration)
{
  ...

  if(userExists)
  {
      const string errorMessage = "User already exist. You can <a href="/account/login">login</a> ...";
      ModelState.AddModelError("Email", errorMessage);
      return View("Register", registration);
  }
}

But when I try to output this message in view I do not get what I expect. I get html markup like plain text. I've already tried:

但是当我尝试输出这条消息时,我没有得到我所期望的。我得到像纯文本一样的 html 标记。我已经试过了

@using(Html.BeginForm())
{
<div>@Html.TextBoxFor(m => m.Email)            
@{
   @Html.ValidationMessageFor(m => m.Email)

   ...

   @Html.Raw(Html.ValidationMessageFor(m => m.Email))

   ...

   string validationMessage = Html.ValidationMessageFor(m => m.Email).ToString();
   @Html.Raw(validationMessage)

   ...

   string validationMessage = Html.ValidationMessageFor(m => m.Email).ToHtmlString();
   @Html.Raw(validationMessage)

   ...

   string validationMessage = Html.ValidationMessageFor(m => m.Email).ToString();
   @(new HtmlString(validationMessage))

   ...

   string validationMessage = Html.ValidationMessageFor(m => m.Email).ToHtmlString();
   @(new HtmlString(validationMessage))

   ...

   string validationMessage = Html.ValidationMessageFor(m => m.Email).ToString();
   @(new MvcHtmlString(validationMessage))

   ...

   string validationMessage = Html.ValidationMessageFor(m => m.Email).ToHtmlString();
   @(new MvcHtmlString(validationMessage))

}
</div>
}

采纳答案by Brett

@Html.Raw(HttpUtility.HtmlDecode(Html.ValidationMessageFor(m => m.Email).ToHtmlString()))

Isn't pretty though

虽然不漂亮

回答by galbani

Read this post for evaluate errors

阅读这篇文章以评估错误

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

如何在验证集合 asp.net mvc 中添加验证错误?

In your view

在你看来

Html.ValidationMessage("Email")

Html.ValidationMessage("电子邮件")

回答by Steviebob

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

在自己使用 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:

我最终做的是在 modelstate.AddError() 中添加我的验证消息部分,并分别向 ViewData 添加具有我想要呈现的 Html 的位,如下所示:

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

ViewData["myKey"]="我的 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.

感觉很丑陋,可能有更好的方法来做到这一点,但对于我非常有限的需求,这非常符合要求。