MvcContrib 网格和显示/编辑模板

发布于 2024-09-02 00:45:00 字数 596 浏览 7 评论 0原文

有没有人遇到过将 ./Views/Shared/DisplayTemplates 和 ./Views/Shared/EditTemplates 与 MvcContrib.UI 网格一起使用的好解决方案?

我想我可以连接一个 CustomItemRenderer,但我更愿意能够做类似的事情:

<% Html.Grid<Person>(Model.People)
         .Sort(new GridSortOptions {Column = Model.Column, Direction = Model.Direction})
         .Columns(column =>
         {
           column.For(e=>e.Name);
           column.DisplayFor(e=>e.StartDate); // <-- I'd like to do this for DateTime.asxc
         }).Render();
%>

网格中可能已经有一些东西可以做到这一点,但我只是还没有找到它。任何帮助将不胜感激。

谢谢,

哈尔

Has anyone come across a good solution for using ./Views/Shared/DisplayTemplates and ./Views/Shared/EditTemplates with the MvcContrib.UI Grid?

I guess I could wireup a CustomItemRenderer, but I would much rather be able to do something like:

<% Html.Grid<Person>(Model.People)
         .Sort(new GridSortOptions {Column = Model.Column, Direction = Model.Direction})
         .Columns(column =>
         {
           column.For(e=>e.Name);
           column.DisplayFor(e=>e.StartDate); // <-- I'd like to do this for DateTime.asxc
         }).Render();
%>

There may already be something in Grid to do this and I just haven't found it yet. Any help would be greatly appreciated.

Thanks,

Hal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小情绪 2024-09-09 00:45:00

我通过修改 GridRenderer 和 GridColumn 类中的源代码实现了这一点。 GridColumn 中的更改是添加以下函数:

public object GetRawValue(T instance)
{
  if (!_cellCondition(instance))
  {
    return null;
  }

  var value = _columnValueFunc(instance);
  return value;
}

然后 GridRenderer 中的更改是在 RenderItem 函数中进行如下更改:

 protected virtual void RenderItem(GridRowViewData<T> rowData)
{
  BaseRenderRowStart(rowData);

  HtmlHelper<T> html = new HtmlHelper<T>(Context, new ViewPage());
  foreach (var column in VisibleColumns())
  {
    //A custom item section has been specified - render it and continue to the next iteration.
    if (column.CustomItemRenderer != null)
    {
      column.CustomItemRenderer(new RenderingContext(Writer, Context, _engines), rowData.Item);
      continue;
    }

    RenderStartCell(column, rowData);

    var cellValue = column.GetRawValue(rowData.Item);
    if (cellValue != null)
    {
      //RenderText(cellValue.ToString());         
      MvcHtmlString value = html.DisplayFor<T, object>(m => cellValue);
      string str = (value.ToString() == string.Empty) ? cellValue.ToString() : value.ToString();
      if (column.HtmlEncode == true)
      {
        str = (value.ToString() == string.Empty) ? HttpUtility.HtmlEncode(cellValue.ToString()) : value.ToHtmlString();
      }
      RenderText(str);
    }

    RenderEndCell();
  }

  BaseRenderRowEnd(rowData);
}

一旦执行此操作,网格将使用找到的模板。我只使用 DisplayTemplates 进行了测试,因为这就是我所需要的,但它也应该适用于 EditorTemplates,只需稍作更改。

I have acheived this by modifying the source code in the GridRenderer and GridColumn classes. The change in GridColumn is to add the following function:

public object GetRawValue(T instance)
{
  if (!_cellCondition(instance))
  {
    return null;
  }

  var value = _columnValueFunc(instance);
  return value;
}

Then the change in GridRenderer is in the RenderItem function to be changed as follows:

 protected virtual void RenderItem(GridRowViewData<T> rowData)
{
  BaseRenderRowStart(rowData);

  HtmlHelper<T> html = new HtmlHelper<T>(Context, new ViewPage());
  foreach (var column in VisibleColumns())
  {
    //A custom item section has been specified - render it and continue to the next iteration.
    if (column.CustomItemRenderer != null)
    {
      column.CustomItemRenderer(new RenderingContext(Writer, Context, _engines), rowData.Item);
      continue;
    }

    RenderStartCell(column, rowData);

    var cellValue = column.GetRawValue(rowData.Item);
    if (cellValue != null)
    {
      //RenderText(cellValue.ToString());         
      MvcHtmlString value = html.DisplayFor<T, object>(m => cellValue);
      string str = (value.ToString() == string.Empty) ? cellValue.ToString() : value.ToString();
      if (column.HtmlEncode == true)
      {
        str = (value.ToString() == string.Empty) ? HttpUtility.HtmlEncode(cellValue.ToString()) : value.ToHtmlString();
      }
      RenderText(str);
    }

    RenderEndCell();
  }

  BaseRenderRowEnd(rowData);
}

Once you do this the grid will use the templates found. I have only tested with DisplayTemplates as this is all I need but it should also work for EditorTemplates with a slight change.

夏末的微笑 2024-09-09 00:45:00

我创建了这个扩展方法:

public static IGridColumn<Column> DisplayFor<Column, Model, Value>(this ColumnBuilder<Column> column, HtmlHelper<Model> html, Func<Column, Value> getValueFunc)
    where Column : class
{
    return column.Custom(o =>
                             {
                                 var value = getValueFunc(o);
                                 return MvcHtmlString.Create("<span>" + html.DisplayFor(modelItem => value) + "</span>");
                             });
}

用法如下所示:

column.DisplayFor(Html, sector => sector.Created);

I have created this extension method:

public static IGridColumn<Column> DisplayFor<Column, Model, Value>(this ColumnBuilder<Column> column, HtmlHelper<Model> html, Func<Column, Value> getValueFunc)
    where Column : class
{
    return column.Custom(o =>
                             {
                                 var value = getValueFunc(o);
                                 return MvcHtmlString.Create("<span>" + html.DisplayFor(modelItem => value) + "</span>");
                             });
}

Usage looks like following:

column.DisplayFor(Html, sector => sector.Created);
往日 2024-09-09 00:45:00

实际上你可以这样做:

column
    .For(model => model.StartDate)
    .Partial("~/Views/Shared/DisplayTemplates/YourModelName.ascx");

这种方法的问题是整个模型将传递给部分模型,而不仅仅是属性。

Actually you could do this:

column
    .For(model => model.StartDate)
    .Partial("~/Views/Shared/DisplayTemplates/YourModelName.ascx");

The problem with this approach is that the whole model will be passed to the partial and not simply the property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文