0

I am using MVC and razor for a style Guide. I need to display the code in the view so others can use it on the site. I need to display the code without the Razor helper executing the function.

I am trying to do this via a Razor helper. I want this helper to render the button as well as display the exact MVC Razor helper code in the view (rather than have it display the outputted HTML from the helper).

@helper ShowCode() { 
        <div>
            @Html.TextBox("textBox")
        </div>
    }

Then I call the helper below and try and display the code:

   // render the button
   @ShowCode()

        // display the button code
        <pre>
            <code>
                @ShowCode().ToString()
            </code>
        </pre>

However, this displays the following: enter image description here

I need it to display the following code instead of the HTML:

 <div>
    @Html.TextBox("textBox")
 </div>

How can I display the code within the Razor helper without it being outputted as html? (I want to display exactly what is in the Razor helper). Is this possible?

1 Answer 1

1

You can't actually do it like that. Razor returns HTML there is no way to get actual Razor template code as string.

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

Comments

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.