2012-11-09 33 views

回答

2

简短的回答是不,没有办法在当前实现中设置tbody的属性。

但是你可以自己实现这个功能:

你只需要从GridRenderer类实现自己的RenderBodyStart评判的版本。

有alredy叫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)); 
     } 
    } 

而且在你看来不是调用Render()可以使用RenderUsing方法,你可以指定你的自定义呈现:

@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> 

您应该注意,这只是一个快速和肮脏的解决方案,以显示什么是可能的,并且有更多的扩展点可用于使属性传递更好。

+0

谢谢!其实我发现这个链接http://johnnycoder.com/blog/2010/06/12/render-mvccontrib-grid-with-no-header-row/它解释了你的相同的解决方案。 –

相关问题