Razor Alternative Html.Raw ()

My html prints in a method. Here is the code:

@Html.Raw(Html.GenerateTabs())  //works -- but is inconvinent

I really did not want to do every such method. This is how I wanted to do it. But that does not work. When I run HTML html codes in a browser.

@Html.GenerateTabs()   //prints html in the broswer


<text>@Html.GenerateTabs()</text>  //prints html in the broswer

Is there a favorable way for a razor?

+5
source share
4 answers

If your code outputs MvcHtmlString instead of a string, it will be output correctly with the HTML it contains.

You create an MvcHtmlString with the static factory method that it has from an HTML string.

public MvcHtmlString OutputHtml() {
    return MvcHtmlString.Create("<div>My div</div>");
}

then in sight:

@OutputHtml()
+11
source

Razor encodes by default. So far, I have not found at the presentation or application level to disable it.

, ?

    public static MvcHtmlString RawHtml(this string original)
    {
        return MvcHtmlString.Create(original);
    }

...

@Html.GenerateTabs().RawHtml();
+1

GenerateTabs MvcHtmlString.

It looks like another post here, but why go through another raw html output method and not just specify Html.Raw. IE I am not a supporter of another method, as was done below, but just make sure GenerateTabs returns an MvcHtmlString.

+1
source

You might want to create an assistant in the App_Code folder. The code compiles and is available for all your razor views.

Add say say SiteHelper.cshtml and add helper method

@helper GenerateTabs()
{
   <div>blah</div>
}

Then use it in any razor mode as

@SiteHelper.GenerateTabs()
+1
source

All Articles