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
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.
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.
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.
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.
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)
You can use <span>I want to write in@(myVariable)side</span>
Edit: Aww.. should have known better than to answer this question ^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With