我想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"><a href="/Contents">Home</a></li> <li><a href="/Home/About">About</a></li> <li><a href="/Home/Contact">Contact</a></li> <li class="options no-display"><a href="#">Show toolbar</a></li> <li class="active"><a href="/Admin/Home">CMS</a></li>
我想打印 HTML 标签而不是文本。
我的错误在哪里?