MVC Razor View: Output text as both raw and HTML p

2019-08-06 06:56发布

问题:

I am having a hard time figuring out how to do this...

I am essentially saving a huge blog post in a property called "Body" in a class called "Post". In body I will have various things like

<p> Hello world </p>
<p> Some random paragraph </p>
<codeblock> Here is an example of a basic HTML page
<html>
<body>
<h1> Hello Guys ! </h1>
</body>
</html>
</codeblock>

Then I want to have a code block and thus I want the HTML/CSS/Javascript/etc to just be parsed to the page as HTML encoded/decoded so I literally want the tags and angle brackets to show up on the page instead of being parsed as whatever they are.

I also have a HTML tag called which is ended by . It's nothing special it just indents and adds some specific CSS with it. I want the markup before the and after the tag to render the HTML tags as necessary.

Currently I am literally outputting the contents of the Body property using

@Html.Raw(post.Body)

Nothing special when I save it to the DB:

@Html.TextAreaFor(model => model.Body)

回答1:

So for those that are still having this issue, this is how I resolved it.

1) I included prettyprint. See link below

https://google-code-prettify.googlecode.com/svn/trunk/README.html

2) When editing a post I just add the following code //Bunch of code

Example: var http = require("http");

var server = http.createServer(function(req, res){
console.log(req.url);
resp.write("<html><body>" + req.url + "</html></body>");
resp.end();
});

server.listen(3000);

</pre>

In my Razor View I have the following code:

<div class="blog margin-bottom-40" onload="prettyPrint()">
//Bunch of other code up here for my view
    <div class="blogpost">
        @Html.Raw(post.Body)
    </div>
</div>

My blog is www.techiejs.com

Feel free to have a look and if you need another file from my solution let me know. Currently my git repository is private.