2

There are few questions on this topic https://stackoverflow.com/questions/23498689/handle-a-pre-filtered-list-with-grid-mvc and grid.mvc use filtered Result in Controller but no answer.

I am using a basic example of the Grid.MVC with MVC 4

@using GridMvc.Html

@Html.Grid(Model,"_MyCustomGrid").Columns(columns =>
          {
                columns.Add(foo => foo.Title).Titled("Custom column title").SetWidth(110);
                columns.Add(foo => foo.Description).Sortable(true);
          }).WithPaging(20)

Which creates a grid that looks like this -

enter image description here

Once the user filters the data in the grid I would like them to be able to export this to excel. I have read the source but I can't see an event I can plug into.

Community
  • 1
  • 1
Peter
  • 4,493
  • 6
  • 41
  • 64

1 Answers1

0

The solution I have found was to create a controller and view that return a table with a XLS content type. Unfortunately you get an warning message when you open up the result in Excel so it's not perfect but it works.

e.g.

// Controllers/TaskController.cs
public virtual ActionResult Export () {
    var tasks = repository.GetAllTasks();

    Response.AddHeader("Content-Disposition", "attachment; filename=Tasks.xls");
    Response.ContentType = "application/ms-excel";

    return PartialView("Export", tasks);
}

// /Views/Task/Export.cshtml
@model IEnumerable<OpuzEleven.Models.Task>
@using GridMvc.Html;


@(Html.Grid(Model, "_GridExcel")
    .Named("TaskGrid")
    .AutoGenerateColumns()
    .Columns(columns => columns.Add(c => c.UserProfile.FullName).Titled("Created by"))
    .Sortable(true)
    .Filterable(true))

// /Shared/_GridExcel.cshtml
@using GridMvc.Columns
@model GridMvc.IGrid

@if (Model == null) { return; }
@if (Model.RenderOptions.RenderRowsOnly) {
  @RenderGridBody();
} else {
  <div class="grid-mvc" data-lang="@Model.Language" data-gridname="@Model.RenderOptions.GridName" data-selectable="@Model.RenderOptions.Selectable.ToString().ToLower()" data-multiplefilters="@Model.RenderOptions.AllowMultipleFilters.ToString().ToLower()">
    <div class="grid-wrap">
      <table class="table table-striped grid-table">
        @* Draw grid header *@
        <thead>
          @RenderGridHeader()
        </thead>
        <tbody>
          @RenderGridBody()
        </tbody>
      </table>
    </div>
  </div>
}

@helper RenderGridBody () {
  if (!Model.ItemsToDisplay.Any()) {
    <tr class="grid-empty-text">
      <td colspan="@Model.Columns.Count()">
        @Model.EmptyGridText
      </td>
    </tr>
  } else {
    foreach (object item in Model.ItemsToDisplay) {
      <tr class="grid-row @Model.GetRowCssClasses(item)">
        @foreach (GridMvc.Columns.IGridColumn column in Model.Columns) {
          @column.CellRenderer.Render(column, column.GetCell(item))
        }
      </tr>
    }
  }
}

@helper RenderGridHeader () {
  <tr>
    @foreach (GridMvc.Columns.IGridColumn column in Model.Columns) {
      <th>@column.Title</th>
    }
  </tr>
}

NB: So that links pass on the parameters used to filer the grid use this MVC ActionLink add all (optional) parameters from current url

Community
  • 1
  • 1
Peter
  • 4,493
  • 6
  • 41
  • 64