TheJackal
TheJackal

Reputation: 435

percentage tags in cshtml

Following along on a tutorial in MVC, would you tell me the alternative to this in a ASPX page

<div>
    <%=ViewData["CurrentTime"]%>
</div> 

In a cshtml page, the default view type in MVC 4 on VS2013. When I try the above, the literal meaning is displayed. i.e. ViewData["CurrentTime"] instead of the value DateTime.now

Upvotes: 2

Views: 2602

Answers (2)

Satpal
Satpal

Reputation: 133423

When you using a cshtml. You are working with Razor engine.

In Razor engine, You need to use @ like

@ViewData["CurrentTime"] 

<%=ViewData["CurrentTime"]%> is a ASP.Net engine code

Upvotes: 5

Paweł Bejger
Paweł Bejger

Reputation: 6366

There are two engines in MVC: Razor and ASP.NET WebForms.

.cshtml filles are the files using Razor engine and this is a default engine of ASP.NET MVC from the third version.

The syntax you are using would work with the old WebForms engine.

So you should write:

@ViewData["CurrentTime"]

in case you use ViewData or

@ViewBag.CurrentTime

in case you use ViewBag which is more common when using Razor view engine and in general newer version of ASP.NET MVC .

Upvotes: 4

Related Questions