关于此主题https://stackoverflow.com/questions/23498689/handle-a-pre-filtered-list-with-grid-mvc和grid.mvc use filtered Result in Controller的问题很少,但没有答案。
我正在使用Grid.MVC和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)
这会创建一个看起来像这样的网格 -
一旦用户过滤了网格中的数据,我希望他们能够将其导出为excel。我已阅读了该来源,但我无法看到可插入的事件。
答案 0 :(得分:0)
我找到的解决方案是创建一个控制器和视图,返回一个具有XLS内容类型的表。不幸的是,当您在Excel中打开结果时会收到一条警告消息,因此它并不完美,但它可以正常工作。
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>
}
注意:所以链接传递用于编译网格的参数使用此MVC ActionLink add all (optional) parameters from current url