我目前正在开发一个注册页面。当用户已经存在时,我想在电子邮件字段的错误消息中为用户提供登录和重置密码链接。在控制器中,我有:
[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);
}
}
但是当我尝试在视图中输出此消息时,我没有得到我期望的结果。我得到像纯文本一样的 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>
}