Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 Razor Syntax for straight text output?

Using Razor how/can you write straight text with out wrapping it in some type of html tag?

Example (This works but adds extra span tags):

@{ var foo = true; } @if(foo) { <span>Yes</span> } else { <span>No</span> } 

I'd like to keep my final markup as clean as possible and not have the extra tags.

Thanks!

like image 689
matto0 Avatar asked Mar 27 '11 18:03

matto0


People also ask

What is Razor syntax in MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.

How do I render a razor view to string?

Listing 1: Rendering a Razor View to String from within a Controller. The RenderViewToString() method works by receiving a controller context and virtual view path (i.e., ~/views/item/page. cshtml ) and optional model data that are passed to the view.

What is Razor syntax?

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.

What is Razor in MVC What are the main Razor syntax rules?

Razor is a simple programming syntax for embedding server code in web pages. Razor syntax is based on the ASP.NET framework, the part of the Microsoft.NET Framework that's specifically designed for creating web applications.


1 Answers

use the <text> tags

@{ var foo = true; } @if(foo) { <text>Yes</text> } else { <text>No</text> } 

The <text> tag signals to the razor view engine to write the contents to the output.

Alternatively, you can use @:

@{ var foo = true; } @if(foo) { @:Yes } else { @:No } 
like image 104
Russ Cam Avatar answered Sep 18 '22 18:09

Russ Cam