SomeObject record = new SomeObject();
record.value1 = 1;
record.value2 = "hello";
<td><input type="checkbox" id="indicator_@record.value1_@record.value2" /><td>
Run Code Online (Sandbox Code Playgroud)
创建id为"indicator_1_hello"的复选框的正确剃刀语法是什么?
尝试这种方式时,它表示该对象不包含value1_的定义(可理解),当我尝试"indicator_@record.value1 @ _ @ record.value2"时,如果遇到名为_的事件的运行时错误背景(再次,可以理解).
编辑:
作为我做过的临时解决方案:
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>
Run Code Online (Sandbox Code Playgroud)
我仍然很好奇你是否可以全部内联.
Tim*_*ora 14
@{
// just for testing
var record = new { value1 = "foo", value2 = "bar" };
}
<input type="checkbox" id="indicator_@( record.value1 + "_" + record.value2 )">
Run Code Online (Sandbox Code Playgroud)
得到: <input type="checkbox" id="indicator_foo_bar">
只需确保您没有构建一个ID,该ID可以通过视图模型的自然层次结构更好地自动生成.大多数情况下,您不需要在视图中手动构建ID.