【问题标题】:Rendering a display name containing HTML that's stored in Resource file呈现包含存储在资源文件中的 HTML 的显示名称
【发布时间】:2019-03-14 13:05:55
【问题描述】:

我需要呈现一个存在于资源文件中的 HTML 链接

[Display(Name = "MyPropertyName" ResourceType = typeof(Res.Props))]
public bool MyProperty{ get; set; }

MyPropertyName 资源有一些 HTML:

"Click <a href="bla.com">here</a> to find out more."

我需要在视图上呈现它,以便链接是可点击的。尽管我尝试了哪种方法,HTML 将永远不会呈现,一些尝试(不是确切的语法,但你明白了要点)将显示原始 HTML,而有些则根本不显示任何内容。

var htmlLabel = Html.LabelFor(m => m."MyPropertyName" ).ToString()
Html.Raw(htmlLabel)

Html.Raw(Html.LabelFor(m => m."MyPropertyName" ).ToString())
Html.Raw(Html.LabelFor(m => m."MyPropertyName")

MvcHtmlString.Create(htmlLabel);
Html.Raw(MvcHtmlString.Create(htmlLabel))

等等

我发现了一个关于获取控制器上的属性信息并通过 ViewBag 传递它然后用 HTML.Raw 呈现它的建议,所以我关注了this here(以及其他一些),但该代码只提取了名称属性“MyPropertyName”,而不是资源文件中的字符串。

有什么想法吗?谢谢。

【问题讨论】:

    标签: c# asp.net-mvc razor html-helper


    【解决方案1】:

    好吧,在最后一次愚蠢的尝试之后,这突然决定起作用。对于将来遇到此类问题的任何人:

    应用程序有一个StronglyTypedResourceBuilder 类,其中包含我需要的所有数据。所以下面的工作没有问题:

    @using StringResources = Namespace.Resources.StringResources
    
    ...
    
    @Html.CheckBoxFor(m => m.MyPropertyName)
    @Html.Raw(@StringRes.MyPropertyName)
    
    ...
    

    浪费了一个上午。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-30
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多