views:

416

answers:

2

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

+1  A: 

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.

Darin Dimitrov
+3  A: 

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.

g00fy