0

I asked this question here on the forum for static link helper, but I got no answers yet. So I decided to create my own helper.

I'm trying to create a helper for a static link

<a href='xx'>yy</a>

but is displaying the HTML code.

Using:

<div>
@Html.Link("www.google.com", "Google")
</div>

Result:

<a href="www.google.com">Google</a> 

See my class:

public static class BindHelper
{
    public static TagBuilder Link(this HtmlHelper helper, string targetUrl, string text)
    {
        TagBuilder imglink = new TagBuilder("a");
        imglink.MergeAttribute("href", targetUrl);
        imglink.InnerHtml = text;
        return imglink;
    }
}

How to create my own helper? Already researched on several sites and some extended the method returns a string in the other class TagBuilder but both ways it displays the HTML code on page

1 Answer 1

4

Return an MvcHtmlString. That is what MVC does internally. It (somewhat) implements IHtmlString which tells the HTML encoder not to reencode the value.

Also add your namespace to the build configuration section of the web.config.

Sign up to request clarification or add additional context in comments.

2 Comments

but in cases where the project is not to MVC?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.