Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC inline Razor variable

In pre-Razor MVC I could write this in a view:

<span>I want to write in<%= myVariable %>side</span>

In Razor, of course I can't

<span>I want to write in@myVariableside</span>

bacause the template engine would look for the variable @myVariableside. How to solve this? Thanks

like image 234
pistacchio Avatar asked Mar 08 '12 08:03

pistacchio


People also ask

Does ASP.NET MVC use Razor?

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. It's just that ASP.NET MVC has implemented a view engine that allows us to use Razor inside of an MVC application to produce HTML.

How do I declare a variable in Razor page?

To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.

What is Razor in MVC with example?

Razor is a templating engine and ASP.NET MVC has implemented a view engine which allows us to use Razor inside of an MVC application to produce HTML. However, Razor does not have any ties with ASP.NET MVC. Now, Razor Syntax is compact which minimizes the characters to be used, however it is also easy to learn.

Can you mix Razor pages and MVC?

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

First - have you tried it?

Secondly - if the interpreter has issues - you can try @(myVariableside).

Equally if the variable name you're talking about is @name - then once you're inside the parentheses everything's fine, because the interpreter knows it's parsing C#/VB: @(@myVariableside)

like image 86
Andras Zoltan Avatar answered Sep 28 '22 05:09

Andras Zoltan


You can use <span>I want to write in@(myVariable)side</span>

Edit: Aww.. should have known better than to answer this question ^^

like image 28
Brunner Avatar answered Sep 28 '22 03:09

Brunner