Reputation: 16764
I would like to render li
items using TagBuilder.
My function
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);
}
When I call using the following code:
@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)
The results is printed as text NOT html tag.
<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>
I want to print the HTML tag not text.
Where is my mistake ?
Upvotes: 10
Views: 25681
Reputation: 51
You must return a MvcHtmlString from method
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));
}
Upvotes: 5
Reputation: 61
For situations like this, I have a good friend in the static method HtmlDecode of the HttpUtility class. Try: return MvcHtmlString.Create(HttpUtility.HtmlDecode(li.ToString(TagRenderMode.Normal)));
HTH
Upvotes: 0
Reputation: 3847
Use @Html.Raw(Html.RenderListTag("CMS", "Index", "Home", true))
Upvotes: 3
Reputation: 16764
I found my mistake :)
I used
li.SetInnerText(anchor.ToString(TagRenderMode.Normal));
The correct way is
li.InnerHtml = anchor.ToString(TagRenderMode.Normal);
I changed type of my function from string
to MvcHtmlString
like:
public static MvcHtmlString RenderListTag(this HtmlHelper helper, string labelText, string action, string controller, bool isAdmin, string listCssClass = "")
And the return of function is:
return MvcHtmlString.Create(li.ToString());
Now, works.
Upvotes: 29
Reputation: 6528
Try changing the last line to:
return helper.Raw(li.ToString(TagRenderMode.Normal)).ToHtmlString();
Upvotes: 0