Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 using Razor - use conditional expression together with the HTML output

I've been bugged with this for many days...(I'm in process of studying ASP.NET MVC 3)

In this link -- Razor If/Else conditional operator syntax -- is said that the only valid syntax for conditional expression in Razor engine is @(x?y:z)

Alright. Now, how do I write HTML in this conditional expression? I can't use Razor here, following code results in invalid syntax error.

@(item.Manager == null ? @:<i>unassigned</i> : item.Manager.Name)

After some research I discovered HtmlWriter or Html.Raw but neither of them, nor their methods .toString() or .toHtmlString() help because they are not of string but of IHtmlString type.

Thanks for reply!

like image 875
Mirek Avatar asked Jul 20 '11 13:07

Mirek


People also ask

Which ASP.NET MVC object generate the HTML output that displays in a web browser?

Typically, Razor rendering in ASP.NET MVC is reserved purely for view rendering and generation of HTML output as part of an MVC request.

What is Razor in MVC with example?

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. You can use it anywhere to generate output like HTML.

What is Razor in 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.


1 Answers

@(item.Manager == null ? new HtmlString("<i>unassigned</i>") : new HtmlString( item.Manager.Name) )
like image 160
ingo Avatar answered Oct 11 '22 11:10

ingo