简体   繁体   中英

How to display razor and C# code in view in MVC

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: 在此处输入图片说明

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM