7

Example:

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

Result:

$'{34645}_{71}'

Expected:

34645_71

Update: Had to Enable C#6 and install the appropriate package for @smdrager's last two techniques to work. In VS2015>>Click Project Menu>>Click Enable 6#

usefulBee
  • 9,250
  • 10
  • 51
  • 89

1 Answers1

14

In the example you provided, string interpolation is not necessary. Using standard razor syntax, you can get the result you want with:

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

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

The equivallent with interpolation would be:

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

But you lose out on HTML encoded to prevent script injection (though this seems unlikely with the data types you are dealing with).

Edit:

If you want to get completely wacky, you can mix both.

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

But that is more verbose and difficult to read.

smdrager
  • 7,327
  • 6
  • 39
  • 49
  • Just tried the first technique, @item.CustomerID_ does not work. CustomerID would not be recognized because of the underscore. – usefulBee Sep 20 '16 at 19:50
  • @DStanley, right and verified..thx! I think this needs to be edited in the answer. – usefulBee Sep 20 '16 at 19:56
  • 2
    Sorry about that. I corrected the answer. Sometimes you need to use parentheses to clear up confusion for the razor parser. – smdrager Sep 20 '16 at 19:57