简短的回答是不,目前的实现无法在tbody上设置属性。
但是你可以自己实现这个功能:
您只需要从GridRenderer 类中实现您自己版本的RenderBodyStart 方法。
已经有一个名为 HtmlTableGridRenderer 的 GridRenderer 实现,您可以在此基础上进行构建:
public class BodyWithAttributesHtmlTableGridRenderer<T>
: HtmlTableGridRenderer<T> where T : class
{
private readonly IDictionary<string, object> bodyAttributes;
public BodyWithAttributesHtmlTableGridRenderer(
IDictionary<string, object> bodyAttributes)
{
this.bodyAttributes = bodyAttributes;
}
protected override void RenderBodyStart()
{
string str = BuildHtmlAttributes(bodyAttributes);
if (str.Length > 0)
str = " " + str;
RenderText(string.Format("<tbody{0}>", str));
}
}
在您看来,您可以使用RenderUsing 方法来指定您的自定义渲染器,而不是调用Render():
@Html.Grid(Model))
.RenderUsing(new BodyWithAttributesHtmlTableGridRenderer<MyModel>(
new Dictionary<string, object>(){{"data-bind", "foreach: people"}}))
生成的 html 看起来像这样:
<table class="grid">
<thead>
<tr>
<th>Prop</th>
</tr>
</thead>
<tbody data-bind="foreach: people">
<tr class="gridrow">
<td>1</td>
</tr>
<tr class="gridrow_alternate">
<td>2</td>
</tr>
</tbody>
</table>
您应该注意,这只是一个快速而肮脏的解决方案,以展示可能的情况,并且您可以使用更多扩展点来使属性传递更好。