I am stuck with HtmlHelper method, which is not able to render in the browser i know that it si very silly issue but not finding out ...
using System.Web.Mvc;
namespace RoleTest.Helpers
{
public static class HtmlHelperExtensions
{
public static string Span(this HtmlHelper html, string text)
{
var builder = new TagBuilder("span");
builder.GenerateId("firstName");
builder.SetInnerText(text);
return builder.ToString(TagRenderMode.Normal);
}
}
}
.cshtml
@using RoleTest.Helpers
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>
</p>
@Html.Span("This is Index Page")
Interesting thing is in the browser its coming like this
<span id="firstName">This is Index</span>
abhijithmani...
Member
398 Points
169 Posts
HTML Helper rendering issue..
Apr 15, 2012 11:58 AM|LINK
Hi,
I am stuck with HtmlHelper method, which is not able to render in the browser i know that it si very silly issue but not finding out ...
using System.Web.Mvc; namespace RoleTest.Helpers { public static class HtmlHelperExtensions { public static string Span(this HtmlHelper html, string text) { var builder = new TagBuilder("span"); builder.GenerateId("firstName"); builder.SetInnerText(text); return builder.ToString(TagRenderMode.Normal); } } } .cshtml @using RoleTest.Helpers @{ ViewBag.Title = "Home Page"; } <h2>@ViewBag.Message</h2> <p> To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a> </p> @Html.Span("This is Index Page")francesco ab...
All-Star
20888 Points
3277 Posts
Re: HTML Helper rendering issue..
Apr 15, 2012 12:16 PM|LINK
if you retur it as string it is automatically Html encoded causing the effect you notice.
You must return
return MvcHtmlSrting.Create(builder.ToString(TagRenderMode.Normal));
Mvc Controls Toolkit | Data Moving Plug-in Videos
abhijithmani...
Member
398 Points
169 Posts
Re: HTML Helper rendering issue..
Apr 15, 2012 12:33 PM|LINK
" Cannot implicitly convert type 'System.Web.Mvc.MvcHtmlString' to 'string' " is throwing if you try to that..
francesco ab...
All-Star
20888 Points
3277 Posts
Re: HTML Helper rendering issue..
Apr 15, 2012 12:35 PM|LINK
Clearly you have also to change the type returned by your method from string to MvcHtmlString
Mvc Controls Toolkit | Data Moving Plug-in Videos