10

我想li使用 TagBuilder 呈现项目。

我的功能

public static string RenderListTag(this HtmlHelper helper, string labelText, string action, string controller, bool isAdmin, string listCssClass = "")
        {
            string value = string.Empty;

            TagBuilder li = new TagBuilder("li");
            TagBuilder anchor = new TagBuilder("a");
            UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

            if (string.IsNullOrEmpty(action) || string.IsNullOrEmpty(controller))
            {
                anchor.MergeAttribute("href", "#");
            }
            else
            {
                anchor.MergeAttribute("href", urlHelper.Action(action, controller, new
                {
                    area = isAdmin ? "Admin" : ""
                }));
            }

            anchor.SetInnerText(labelText);

            if (action.IsEqualWith(helper.ViewContext.RouteData.Values["action"].ToString()))
            {
                li.MergeAttribute("class", "active");
            }

            if (!string.IsNullOrEmpty(listCssClass))
            {
                li.MergeAttribute("class", listCssClass);
            }

            li.SetInnerText(anchor.ToString(TagRenderMode.Normal));

            return li.ToString(TagRenderMode.Normal);
        }

当我使用以下代码调用时:

@Html.RenderListTag("Home", "Index", "Contents", false)
@Html.RenderListTag("About", "About", "Home", false)
@Html.RenderListTag("Contact", "Contact", "Home", false)
@Html.RenderListTag("Show toolbar", "", "", false, "options no-display")
@Html.RenderListTag("CMS", "Index", "Home", true)

结果打印为文本而不是 html 标记。

<li class="active">&lt;a href=&quot;/Contents&quot;&gt;Home&lt;/a&gt;</li> <li>&lt;a href=&quot;/Home/About&quot;&gt;About&lt;/a&gt;</li> <li>&lt;a href=&quot;/Home/Contact&quot;&gt;Contact&lt;/a&gt;</li> <li class="options no-display">&lt;a href=&quot;#&quot;&gt;Show toolbar&lt;/a&gt;</li> <li class="active">&lt;a href=&quot;/Admin/Home&quot;&gt;CMS&lt;/a&gt;</li> 

我想打印 HTML 标签而不是文本。

我的错误在哪里?

4

5 回答 5

29

我发现我的错误:)

我用了

li.SetInnerText(anchor.ToString(TagRenderMode.Normal));

正确的方法是

li.InnerHtml = anchor.ToString(TagRenderMode.Normal);

我将函数的类型从如下更改stringMvcHtmlString

public static MvcHtmlString RenderListTag(this HtmlHelper helper, string labelText, string action, string controller, bool isAdmin, string listCssClass = "")

函数的返回是:

return MvcHtmlString.Create(li.ToString());

现在,工作。

于 2013-03-16T14:55:48.143 回答
5

您必须从方法返回一个MvcHtmlString

public static MvcHtmlString RenderListTag(this HtmlHelper helper, string labelText, string action, string controller, bool isAdmin, string listCssClass = "")
{
    string value = string.Empty;

    TagBuilder li = new TagBuilder("li");
    TagBuilder anchor = new TagBuilder("a");
    UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

    if (string.IsNullOrEmpty(action) || string.IsNullOrEmpty(controller))
    {
        anchor.MergeAttribute("href", "#");
    }
    else
    {
        anchor.MergeAttribute("href", urlHelper.Action(action, controller, new
        {
            area = isAdmin ? "Admin" : ""
        }));
    }

    anchor.SetInnerText(labelText);

    if (action.IsEqualWith(helper.ViewContext.RouteData.Values["action"].ToString()))
    {
        li.MergeAttribute("class", "active");
    }

    if (!string.IsNullOrEmpty(listCssClass))
    {
        li.MergeAttribute("class", listCssClass);
    }

    li.SetInnerText(anchor.ToString(TagRenderMode.Normal));

    return new MvcHtmlString(li.ToString(TagRenderMode.Normal));
}
于 2016-10-26T12:41:50.470 回答
3

采用@Html.Raw(Html.RenderListTag("CMS", "Index", "Home", true))

于 2013-03-16T14:52:18.307 回答
0

尝试将最后一行更改为:

return helper.Raw(li.ToString(TagRenderMode.Normal)).ToHtmlString();

于 2013-03-16T14:55:19.147 回答
0

对于这样的情况,我在HttpUtility类的静态方法HtmlDecode中有一个好朋友。尝试:返回 MvcHtmlString.Create(HttpUtility.HtmlDecode(li.ToString(TagRenderMode.Normal)));

高温高压

于 2013-11-22T02:20:17.107 回答