{@item.CustomerID}_{@item.CustomerType}'"> ...">

Using C # 6 String Interpolation in Razor The Right Way

An example :

<button data-value="$'{@item.CustomerID}_{@item.CustomerType}'"></button> 

Result

 $'{34645}_{71}' 

Expected :

 34645_71 

Update : To enable C # 6 and install the appropriate package for @smdrager, you need to use the last two methods. In VS2015 → Click “Project Menu” → Click “Enable” 6 #

+5
source share
1 answer

In the above example, line interpolation is not required. Using the standard razor syntax, you can get the result you want:

 <button data-value="@(item.CustomerID) _@item.CustomerType "></button> 

What will produce <button data-value="34567_123"></button>

The same with interpolation will be:

 @Html.Raw($"<button data-value='{item.CustomerID}_{item.CustomerType}'></button>") 

But you lose the HTML encoded to prevent the script from being injected (although this is unlikely for the data types you are dealing with).

Edit:

If you want to be completely stupid, you can mix both.

 <button data-value="@($"{item.CustomerID}_{item.CustomerType}")"></button> 

But it is more detailed and difficult to read.

+8
source

All Articles