【问题标题】:Multiline cell in Telerik grid (MVC3)Telerik 网格中的多行单元格 (MVC3)
【发布时间】:2011-08-05 13:25:06
【问题描述】:

使用 Telerik MVC3 网格、C#、.Net 2010;

我的剃刀视图中有一个网格:

@(Html.Telerik().Grid<ProductListItem>()
.Name("Grid")
.Columns(columns =>
{
       columns.Bound(o => o.Current.Name).Sortable(true).Filterable(false).Width(150);
       columns.Bound(o => o.Categories).Sortable(true).Filterable(false).Width(200);
       //other column bindings...
})
.DataBinding(dataBinding => dataBinding.Ajax().Select(Model.GridAjaxRequestAction.ActionName, Model.GridAjaxRequestAction.ControllerName))
.Pageable(settings => settings.Total(Model.TotalRow))
.EnableCustomBinding(true)
.Sortable()
.Filterable()

我想要做的是将网格的类别列设置为多行。

一个产品可能有很多类别,因此网格中的类别单元格应该是这样的;

Category0
Category1
Category2

我尝试将类别值与 System.NewLine 和
连接起来,并将此值分配给 ProductListItem.Categories 属性。它没有改变。文本仍然是单行。

提前致谢。

【问题讨论】:

    标签: c# .net telerik-grid telerik-mvc


    【解决方案1】:

    谢谢@nekno。在我的情况下,我想出了这个解决方案。很抱歉一段时间没有回复。

    在视图模型中:

    this.Categories = String.Join("&lt;br&gt;", entity.Categories.Select(o =&gt; o.Current.Name));

    在视图中: columns.Bound(o => o.Categories).ClientTemplate("")

    【讨论】:

      【解决方案2】:

      如果您尝试使用NewLine 进行连接很容易,请尝试使用"&lt;br /&gt;" 而不是System.NewLine

      否则,您的ProductListItem.Categories 属性的数据类型是什么?如果是List&lt;String&gt; 或其他IEnumerable,您可以使用模板列而不是绑定列。您使用item 来引用模板中当前的ProductListItem

      @(Html.Telerik().Grid<ProductListItem>()
      .Name("Grid")
      .Columns(columns =>
      {
             columns.Bound(o => o.Current.Name).Sortable(true).Filterable(false).Width(150);
             columns.Template(
                  @<text>
                      @String.Join("<br />", item.Categories)
                  </text>)
                  .Sortable(true).Filterable(false).Width(200);
             //other column bindings...
      })
      .DataBinding(dataBinding => dataBinding.Ajax().Select(Model.GridAjaxRequestAction.ActionName, Model.GridAjaxRequestAction.ControllerName))
      .Pageable(settings => settings.Total(Model.TotalRow))
      .EnableCustomBinding(true)
      .Sortable()
      .Filterable()
      

      另一种选择可能是在模板列中创建一个表格,并将您的ProductListItem.Categories 保留为List,例如this.Categories = entity.Categories.Select(o =&gt; o.Current.Name).ToList();

      @(Html.Telerik().Grid<ProductListItem>()
          .Name("Grid")
          .Columns(columns =>
          {
                 columns.Bound(o => o.Current.Name).Sortable(true).Filterable(false).Width(150);
                 columns.Template(
                      @<text>
                          <table border=0>
                               @foreach(var category in item.Categories){
                                   <tr><td>@category</td></tr>
                               }
                          </table>
                      </text>)
                      .Sortable(true).Filterable(false).Width(200);
                 //other column bindings...
          })
          .DataBinding(dataBinding => dataBinding.Ajax().Select(Model.GridAjaxRequestAction.ActionName, Model.GridAjaxRequestAction.ControllerName))
          .Pageable(settings => settings.Total(Model.TotalRow))
          .EnableCustomBinding(true)
          .Sortable()
          .Filterable()
      

      【讨论】:

      • ProductListItem.Categories 属性作为连接字符串出现; this.Categories = String.Join("
        ", entity.Categories.Select(o => o.Current.Name));我试过这个和你使用模板建议的建议,但它仍然是一样的:/.
      • @berdem - 您可以在浏览器中查看 HTML 源代码,然后将它显示的包含多个类别的表格单元格粘贴到此处吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多