Razor Syntax - Using Two Variables in a String

SomeObject record = new SomeObject();
record.value1 = 1;
record.value2 = "hello";

<td><input type="checkbox" id="indicator_@record.value1_@record.value2" /><td>

What is the correct razor syntax to create a checkbox with the identifier "indicator_1_hello"?

While trying this method, he says that the object does not contain a definition for value1_ (understandable), and when I tried " indicator_@record.value1 @_ @ record.value2" if an error occurred while executing something named _ not existing in context (again, understandable).

edit:

As a workaround I made:

SomeObject record = new SomeObject();
record.value1 = 1;
record.value2 = "hello";
var combined = String.Format("{0}_{1}", record.value1, record.value2);

<td><input type="checkbox" id="indicator_@combined" /><td>

I'm still curious if you can do all this, however.

+5
source share
2 answers
@{
    // just for testing
    var record = new { value1 = "foo", value2 = "bar" };
}

<input type="checkbox" id="indicator_@( record.value1 + "_" + record.value2 )">

It gives: <input type="checkbox" id="indicator_foo_bar">

, , . .

+12

- , ( CheckboxID) , .

+1

All Articles