在 AddModelError 中添加指向另一个 Action 的链接

DaI*_*mTo 0 c# asp.net asp.net-mvc asp.net-core-mvc

我有一个错误,我想添加一个链接

ModelState.AddModelError(string.Empty, "You must have a confirmed email to log in.");
Run Code Online (Sandbox Code Playgroud)

我想要做的是添加一个指向 SendEmailConfirmationMail 操作的链接,以便在丢失时我可以将其重新发送给他们。

[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> SendEmailConfirmationMail(string email, string returnUrl = null)
    {
  // Stuff removed
    }
Run Code Online (Sandbox Code Playgroud)

我尝试过的

ModelState.AddModelError(string.Empty, 
                         $"You must have a confirmed email to log in. Click to resend conformation email <a href=account/SendEmailConfirmationMail?Email={model.Email}&returnUrl={returnUrl}> resend</a>");
Run Code Online (Sandbox Code Playgroud)

ModelState.AddModelError(string.Empty, $"<a href=\"{Url.Action("SendEmailConfirmationMail", "Account", new { Email = model.Email, returnUrl = returnUrl })}\">Click me</a>");
Run Code Online (Sandbox Code Playgroud)

这效果不太好。

在此输入图像描述

如果我做错了,我愿意接受其他想法。我还在学习asp.net mvc。

从它的表现来看

 @if (error != null)
            {
                <strong>
                    <em> : @Html.Raw(error)</em>
                </strong>
            }
Run Code Online (Sandbox Code Playgroud)

更新:

当我检查页面时。看起来 html 正在被替换。

<li>&lt;a href="/Account/SendEmailConfirmationMail?Email=lilaw@eg.dk"&gt;Click me&lt;/a&gt;</li>
Run Code Online (Sandbox Code Playgroud)

Zab*_*sky 5

如果您控制显示错误的视图,最好在视图本身中格式化错误,因为它是表示逻辑。

在控制器中添加模型错误:

ModelState.AddModelError("EmailNotConfirmedError", string.Empty); // You can specify the error message or ignore it
Run Code Online (Sandbox Code Playgroud)

检查视图中是否存在此特定错误:

<div>
    @if (ViewData.ModelState.ContainsKey("EmailNotConfirmedError"))
    {
        <em>You must have a confirmed email to log in. Click to resend conformation email @Html.ActionLink("resend", "SendEmailConfirmationMail", "Account")</em>
    }
</div>
Run Code Online (Sandbox Code Playgroud)