使用TagBuilder和ASP渲染html代码.. NET MVC 4(使用Razor引擎)
本文关键字:使用 Razor 引擎 MVC 代码 TagBuilder ASP 渲染 html NET | 更新日期: 2023-09-27 18:04:08
我想用TagBuilder渲染li
项。
函数
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)
结果打印为text NOT 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标签而不是文本。
我发现我的错误了:)
我用
li.SetInnerText(anchor.ToString(TagRenderMode.Normal));
正确的方法是
li.InnerHtml = anchor.ToString(TagRenderMode.Normal);
我将函数类型从string
更改为MvcHtmlString
,如:
public static MvcHtmlString RenderListTag(this HtmlHelper helper, string labelText, string action, string controller, bool isAdmin, string listCssClass = "")
,返回函数为:
return MvcHtmlString.Create(li.ToString());
现在作品。
必须从方法
返回一个MvcHtmlStringpublic 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));
}
使用@Html.Raw(Html.RenderListTag("CMS", "Index", "Home", true))
尝试将最后一行更改为:
return helper.Raw(li.ToString(TagRenderMode.Normal)).ToHtmlString();
对于这种情况,我在HttpUtility类的静态方法HtmlDecode中有一个好朋友。Try: return MvcHtmlString.Create(httutility . htmldecode (li.ToString(TagRenderMode.Normal))));
HTH