0

I have created a html helper method like this

  public static class abcExtensions
  {
    public static string Label1(this HtmlHelper helper, string target, string text)
    {
        return String.Format("<label for='{0}'>{1}</label>", target, text);

    }
}

But wne I use it like

@Html.Label1(..)

it render encoded text like this

<label for='aa'>qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq</label>

I dont want to use @Html.Raw

How can I render Html instead of encoded text?

user786
  • 3,902
  • 4
  • 40
  • 72
  • Recommend you study the [source code](https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/LabelExtensions.cs) –  Jul 13 '16 at 09:46
  • @StephenMuecke do you know if I want my helper to connect to model? – user786 Jul 13 '16 at 09:58
  • Sorry, don't understand your comment (the source code shows how to write an extension method) –  Jul 13 '16 at 10:00
  • for example @Html.TextBox(m=>m.username,...) here, I can connect the helper with model data. How can I do this for creating my own helper? – user786 Jul 13 '16 at 10:03
  • yes you can write your own html helpers, see : http://developmentpassion.blogspot.nl/2014/09/creating-custom-html-helper-extensions.html – Ehsan Sajjad Jul 13 '16 at 10:03
  • see this post as well, where we create image link :http://developmentpassion.blogspot.com/2015/07/image-actionlink-html-helper-in-aspnet.html – Ehsan Sajjad Jul 13 '16 at 10:04
  • If you look at the source code it will show how to do that. But what are you trying to differently than the inbuilt `Label()` or `LabelFor()` methods? –  Jul 13 '16 at 10:07
  • @StephenMuecke I want something like @Html.Label1(m=>m.EmployeeName). and My rendered label gets filled with employee name because of m.EmployeeName. How can I do this? – user786 Jul 13 '16 at 10:08
  • @Alex, That does not make sense. A ` –  Jul 13 '16 at 10:11
  • @StephenMuecke I am just asking for learning purpose. lets call it label1For that I am creating. how to do this? – user786 Jul 13 '16 at 10:13
  • 1
    Your now asking a different question :) (and Ehsan Sajjad has answered this one correctly) . But I will give you a couple of links later. –  Jul 13 '16 at 10:22
  • @StephenMuecke ok thanks but please dont forget to give me the links later – user786 Jul 13 '16 at 10:27
  • @StephenMuecke also the mvc resource link you sent me above, can you send me labelExtension class link too on the same resource, later is ok? – user786 Jul 13 '16 at 10:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117196/discussion-between-stephen-muecke-and-alex). –  Jul 13 '16 at 10:51
  • @EhsanSajjad you know the information the link you posted is not detailed enough. After reading it I have no idea what is:ControllerBase ControllerContext,ControllerDescriptor, or ActionDescriptor? Please explain the aboove classes – user786 Jul 13 '16 at 12:42
  • @Alex you can read the source code of those classes if you want to dive more deeper – Ehsan Sajjad Jul 13 '16 at 13:23

2 Answers2

3

You need to return IHtmlString which returns Html-Encoded string like;

public static IHtmlString Label1(this HtmlHelper helper, string target, string text)
{

   return new HtmlString(String.Format("<label for='{0}'>{1}</label>", target, text));

}

HtmlString wraps a string that is assumed to not need HTML encoded.It implements IHtmlString, so when calling HttpUtility.HtmlEncode(htmlString) the string won't be encoded.

You can see this post as well which explains more when should you use HtmlString which says that:

what you should remember is that razor encodes everything by default, but by using MvcHtmlString in your html helpers you can tell razor that it doesn't need to encode it

You can always see the source code as well if you are interested to see how it works.

Community
  • 1
  • 1
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

Make your helper method to return an HtmlString instead of string.

https://msdn.microsoft.com/en-us/library/system.web.htmlstring(v=vs.110).aspx

Claudio Valerio
  • 2,302
  • 14
  • 24