Asp.net mvc 使用HTML内容生成标签

Asp.net mvc 使用HTML内容生成标签,asp.net-mvc,razor,asp.net-mvc-5,label,html-helper,Asp.net Mvc,Razor,Asp.net Mvc 5,Label,Html Helper,使用MVC和Razor,我想为包含HTML的字段创建一个标签,即超链接。但是,当我使用Html.LabelFor()方法时,所有Html都在输出时编码 此屏幕截图显示了期望的结果以及MVC实际输出的内容: 有没有办法为“我的模型”属性生成一个标签,以正确呈现HTML内容? 我的ViewModel: [DisplayName("I accept the <a href=\"/Terms &amp; conditions\">Terms &amp; conditions

使用MVC和Razor,我想为包含HTML的字段创建一个标签,即超链接。但是,当我使用
Html.LabelFor()
方法时,所有Html都在输出时编码

此屏幕截图显示了期望的结果以及MVC实际输出的内容:

有没有办法为“我的模型”属性生成一个标签,以正确呈现HTML内容?

我的ViewModel:

[DisplayName("I accept the <a href=\"/Terms &amp; conditions\">Terms &amp; conditions</a>")]
public bool AcceptedTermsAndConditions { get; set; }
我还尝试将内容作为“labeltext”参数直接传递,但没有成功:

@Html.LabelFor(m => m.AcceptedTermsAndConditions, "I accept the <a href=\"/Terms &amp; conditions\">Terms &amp; conditions</a>")
@Html.LabelFor(m=>m.AcceptedTermsAndConditions,“我接受”)
尝试使用以下方法:

[DisplayName("I accept the <a href='Terms &amp; conditions'>Terms &amp; conditions</a>")]
[DisplayName(“我接受”)]

我明白了。所以不是

@Html.LabelFor(m => m.AcceptedTermsAndConditions)
使用


不是最可爱的,而是解决方案:

@Html.Raw(@HttpUtility.HtmlDecode( Html.LabelFor(x=>x.AcceptedTermsAndConditions).ToString()))

最后,我根据
Html.LabelFor()
的反编译源代码编写了自己的HtmlHelper方法:

而不是原来的

 tagBuilder.SetInnerText(..)
这样提供的labelText就不会以HTML编码结束

我的案例的用法是:

@Html.HtmlLabelFor(m => m.AcceptedTermsAndConditions)

除了引号的变化之外,这与我正在做的有什么不同?我不明白为什么当LabelFor()显然对所有HTML进行编码时,这会起作用。我已经想到了,但这与LabelFor()不同。这样,我将不得不手动使用标签将其包围起来,并且必须使用“魔术字符串”设置
for=“AcceptedTermsAndConditions”
属性。LabelFor的主要优点是,它为您处理for属性,将其设置为与输入标记相同的id!这很有趣@Html.Raw(@HttpUtility.HtmlDecode(Html.LabelFor(x=>x.AcceptedTermsAndConditions).ToString())为什么不直接使用
[DisplayName(“我接受”)]
并在视图中像平常一样将
标记放在
@Html.LabelFor
后面?编辑:我猜该锚不太可能经常更改url。@Esteban,因为这是一个黑客行为?将标签移出标签会导致各种各样的问题,从本地化到不同的语言,链接可能会出现在文本的中间,当我稍后用CSS改变标签的外观时会出现问题。我在为一般情况寻找合适的解决方案。标签标签允许HTML content.fair是有原因的。似乎您可能想使用
@Html.Raw
来显示
标签或(m=>m.AcceptedTermsAndConditions)
。你试过了吗?@Esteban:那行不通,文本在那一点上已经被编码了。Yuri下面的答案将其解码,然后使用Raw()防止再次编码,但我真的不喜欢不必要的encode->decode循环。
public static IHtmlString HtmlLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText = null, object htmlAttributes = null)
{
    string str = labelText;
    var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
    var htmlFieldName = ExpressionHelper.GetExpressionText(expression);
    if (str == null)
    {
        string displayName = metadata.DisplayName;
        if (displayName == null)
        {
            string propertyName = metadata.PropertyName;
            str = propertyName ?? htmlFieldName.Split(new[] {'.'}).Last();
        }
        else
        {
            str = displayName;
        }
    }
    string innerHtml = str;
    if (string.IsNullOrEmpty(innerHtml))
        return MvcHtmlString.Empty;
    var tagBuilder = new TagBuilder("label");
    tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
    tagBuilder.InnerHtml = innerHtml;
    tagBuilder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), true);
    return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
}
 tagBuilder.InnerHtml = ..
 tagBuilder.SetInnerText(..)
@Html.HtmlLabelFor(m => m.AcceptedTermsAndConditions)