Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor doesn't understand unclosed html tags

With RazorViewEngine, I can do this:

if (somecondition) {      <div> some stuff </div> } 

but I can't seem to do this (Razor gets confused):

if (somecondition) {     <div> }  if (someothercondition) {     </div> } 

I have a situation in which I need to put my opening and closing html tags in different code blocks - how can I do this in Razor?

like image 762
sydneyos Avatar asked Jan 26 '11 20:01

sydneyos


People also ask

What is unclosed tag in HTML?

What Is a Void Element? The void elements or singleton tags in HTML don't require a closing tag to be valid. These elements are usually ones that either stand alone on the page ​or where the end of their contents is obvious from the context of the page itself.

How do you find an unclosed tag in HTML?

In the left pane of the code view you can see there <> highlight invalid code button, click this button and you will notice the unclosed div highlighted and then close your unclosed div. Press F5 to refresh the page to see that any other unclosed div are there.

What happens if you don't close HTML tag?

If you don't add a closing tag the browser won't know where it ends. It will take the next tag and think it belongs to the previous tag without the closing tag.

What is razor view HTML?

Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.


2 Answers

Try like this:

if (somecondition) {     @:<div> } 
like image 52
Darin Dimitrov Avatar answered Oct 14 '22 17:10

Darin Dimitrov


To explain Darin's answer, i.e prefixing the HTML like this:

@:<html> 

@: in Razor means 'render something as plain text'

or you can use this, which outputs the HTML as you orginally wrote it (this can also be used to avoid the automatic HTML encoding that Razor does if you're trying to output HTML):

@Html.Raw("<html>") 

(Html.Raw reference from MS - http://msdn.microsoft.com/en-us/library/gg568896(v=vs.111).aspx)

like image 29
Chris Halcrow Avatar answered Oct 14 '22 17:10

Chris Halcrow