C # Razor syntax inside string

This is the code I'm having problems with:

@{int i = 1;} @foreach (var item in Model) { @:<ul id="sortablei" class='droptrue'> i++; } 

How can i use i in sorting id? I tried: @i or @ {i}, but it seems like this is not the case until I work. I could not find the answer to my problem in the Razor Syntax Reference, so now I'm a little stranger.

+6
source share
2 answers

Brackets:

 @:<ul id=" sortable@ (i)" class='droptrue'> 

The expression for the razor is in parentheses; in many cases they are not required, but they are necessary when:

  • the expression (right) is nontrivial (spaces, etc.) and needs help to cover it
  • where without it, it looks like an email address, i.e. abc@def - this one has special processing so as not to break pages with email addresses in them
+8
source

something like that:

 @model System.Generic.Collections.List<MyNameSpace.Product> @{ int i = 1; string sortablei = "abc", droptrue = "abc-cls"; } <ul id="@sortablei" class="@droptrue"> @foreach (var item in Model) { <li>@item.Qty x @item.Name</li> i++; } </ul> 

Here is a short link for your knowledge.


Stitches like me didn’t understand

as Mark said, all you have to do is surround the variable with brackets like

 sortable@ (i) 

I hope this link is somewhat useful, as your specific problem can be found in Explicit expression example

+1
source

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


All Articles