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

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

创建 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>

不过,我仍然很好奇您是否可以内联完成所有操作。

4

2 回答 2

18
@{
    // just for testing
    var record = new { value1 = "foo", value2 = "bar" };
}

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

给出:<input type="checkbox" id="indicator_foo_bar">

Just make sure that you aren't building an ID which would be better auto-generated by the natural hierarchy of your view model. Most of the time, you shouldn't need to manually construct IDs in your view.

于 2013-03-07T18:30:52.050 回答
1

如果您需要这样的东西,我建议将该字段(即 CheckboxID)添加到您的模型中并在服务器端填充它,然后再将其传递给视图。

于 2013-03-07T18:25:19.940 回答