【问题标题】:ASP.NET Razor tag helper inside text string文本字符串中的 ASP.NET Razor 标记助手
【发布时间】:2020-06-20 05:19:54
【问题描述】:

在我的 razor 页面视图中,我正在填充模型以传递给部分,如下所示:

剃刀页面

@{
    if (Model.Success)
    {
        Model.AlertMessage = new AlertMessage()
        {
            Title = "Email Confirmed",
            Description = "Your email has been confirmed, you can now <a href=\"/login\">login</a> to your account."
        };
    }
    else
    {
        Model.AlertMessage = new AlertMessage()
        {
            Title = "Confirmation Failed",
            Description = "Your email could not be confirmed, please <a href=\"/confirmemail\">try again</a>."
        };
    }
}

<div class="row">
<div class="col-md-8 offset-md-2">
    <partial name="_Partials/Alerts/_AlertMessage", model="Model.AlertMessage"/>
</div>

局部视图

@model AlertMessage

<div class="alert">
<div class="row">
    <div class="col-auto align-self-center alert-icon">
    </div>
    <div class="col">
        <span class="alert-title">@Model.Title</span>
        <p class="alert-content">@Html.Raw(@Model.Description)</p>
    </div>
</div>

以上内容有效,但我想知道是否可以在文本字符串中使用 razor tag helper?以某种方式用标签助手 &lt;a asp-page="/Login"&gt; 替换硬编码的锚标签 &lt;a href=\"/login\"&gt;login&lt;/a&gt; 并让它正确呈现 HTML?

【问题讨论】:

  • 我会用一个新的文件链接扩展模型并将其传递给部分视图。

标签: c# asp.net-mvc asp.net-core razor razor-pages


【解决方案1】:
    if (Model.Success)
{
    Model.AlertMessage = new AlertMessage()
    {
        Title = "Email Confirmed",
        Description = "Your email has been confirmed, you can now" + @Html.ActionLink("Login", "Login") + "to your account."
    };
}

【讨论】:

  • 我认为你可以做到以上,检查重载,第一个参数是你的文本然后下一个是动作,你可能需要一个控制器。
  • 是的,这确实有效,但我希望不必为这种情况创建控制器
  • 您的登录操作在哪里进行?
  • 不确定您是否使用实体,但如果是,则为@Html.ActionLink("Login", "Login", "Account")
  • 如果你必须为 get 做一个控制器,它应该真的很短:比如,[HttpGet] [AllowAnonomous] public ActionResult Login() { return View();甚至不需要 Dispose 方法,因为没有数据库握手
猜你喜欢
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 2020-09-15
  • 1970-01-01
  • 2019-11-07
  • 2015-12-27
  • 1970-01-01
相关资源
最近更新 更多