Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 Razor Syntax - Casting

Tags:

So, simple question really. :)

I had this following code with ASPX View Engine:

<strong><%: ((City)Model.Location).Name %></strong> 

Which renders Los Angeles

If i do this with Razor View Engine:

<strong>@((City)Model.Location).Name</strong>                              ^                              | syntax highlighting stops here 

It renders this:

(really huge object name must be .ToString).Name

So, the highlighting cuts off at Location), and it's treating the .Name code i have (which is a property on a City object) as pure HTML.

Why is it deeming the ) as the end of the Razor code block?

Any ideas?

Also - is there a Razor reference which has all the syntax/keywords? (it took me a while to figure out that <% Import Namespace is @using with Razor).

Thanks Guys!

like image 583
RPM1984 Avatar asked Nov 11 '10 06:11

RPM1984


People also ask

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

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.

What is meaning of @: In .NET MVC?

Using @: to explicitly indicate the start of content We are using the @: character sequence to explicitly indicate that this line within our code block should be treated as content.

Can you use MVC with Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.


2 Answers

Does <strong>@(((City)Model.Location).Name)</strong> work? I think it's thinking the expression contained within @() is closed.

like image 98
John Sheehan Avatar answered Sep 24 '22 21:09

John Sheehan


Also, for slightly easier reading...

<strong>@( (Model.Location as City).Name )</strong> 
like image 23
Dean Thomas Avatar answered Sep 22 '22 21:09

Dean Thomas