Changes in ASP.NET MVC 3 and ASP.NET MVC 4 razor syntax?

I created two applications in VS2012

  • Application # 1. MVC 3 , NET 4.5
  • Application # 2. MVC 4 , NET 4.5

Now I open any .cshtml file and add the following code:

<div> @if (true) { @string.Format("{0}", "test") } </div> 

It works great in application # 1 (mvc3), and I see a "test" word. But this does not work in Application # 2 (mvc4).

Can someone explain why this is happening and what needs to be changed?

UPDATE: I just found out another very strange thing. If you replace @ string.format ("some text") with @ String.format ("some text"), everything works fine (note the upper case of the string)

+4
source share
3 answers

We had a similar problem when updating ... it seems that just writing @string.Format("{0}", "test") will no longer write directly to the page, as in MVC3. Instead, you should do something like this:

 <div> @if (true) { Html.Raw(string.Format("{0}", "test")); } </div> 

or

 <div> @if (true) { <text>@string.Format("{0}", "test"))</text> //added @ to evaluate the expression instead of treating as string literal } </div> 
+6
source

Do you use the Razor mechanism?

Since you are in the @if block, you can write: string.Format("{0}", "test") , not @string.Format("{0}", "test") .

Pay attention to @

0
source

In Razor syntax for conditional checking, you can run a block with a single @ sign, and then put your condition inside.

For instance,

  @{var price=20;} <html> <body> @if (price>30) { <p>The price is too high.</p> } else { <p>The price is OK.</p> } </body> </html> 

So, in your case, you use two @ inside the same block. Check this part and it will be resolved.

So, your code block should look like this.

 <div> @if (Model == null) { <p>@string.Format("{0}", "test")</p> } </div> 

Also, if you put the β€œ@” two times, as it is in your code lines, it will give you a hint like the image below. This is a feature of Razor 4.0 syntax. Also you will skip ";" in your line of code.

enter image description here

http://www.w3schools.com/aspnet/razor_cs_logic.asp

0
source

Source: https://habr.com/ru/post/1437335/


All Articles