【问题标题】:How to include mailto link within model error message如何在模型错误消息中包含 mailto 链接
【发布时间】:2013-03-06 13:53:51
【问题描述】:

将 mailto 添加到此 ASP.NET MVC3 代码的正确语法是什么。这不起作用,使用双引号也不起作用:

ModelState.AddModelError("", "We could not locate the email address you entered.  Please check the email address.  If you believe it is correct, please contact us by sending an email to <a href='mailto:support@abc.com'>Support</a> from the email address you are attempting to use.");

【问题讨论】:

  • 你的意思是“不起作用”?你得到什么 HTML?
  • 这是浏览器中显示的内容:发送给支持的电子邮件
  • 所以这意味着文本正在服务器端进行编码.. 担心除了添加全局 JavaScript 将其转换为实际链接之外您无能为力。如果可行,请告诉我,我会提供样品。

标签: asp.net asp.net-mvc asp.net-mvc-3


【解决方案1】:

我不太擅长 Razor,但这暂时可以解决您的问题

@Html.Raw(Html.ValidationSummary().ToString().Replace("[mailto]", "<a href='mailto:support@abc.com'>Support</a>"))

您的消息将包含此内容

ModelState.AddModelError("", "We could not locate the email address you entered.  Please
check the email address.  If you believe it is correct, please contact us by sending an 
email to [mailto] from the email address you are attempting to use.");

链接现在从服务器/控制器添加到视图中,用实际链接替换您的[mailto] 并调用Html.Raw 以在浏览器中呈现 html

【讨论】:

    【解决方案2】:

    尝试使用

    添加这个 NamesPace :Using System.Text

        StringBuilder s=new StringBuilder ();
        s.Append(We could not locate the email address you entered.  Please check the email address.  If you believe it is correct, please contact us by sending an email to ");
        s.Append("<a href='mailto:support@abc.com'>Support</a>");
        s.Append("from the email address you are attempting to use.");
        ModelState.AddModelError("", s.ToString());
    

    【讨论】:

      猜你喜欢
      • 2012-10-16
      • 2017-10-10
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 2017-04-14
      相关资源
      最近更新 更多