This question is locked. New answers and comments are not allowed.
I'm currently using a Telerik Grid for MVC with Ajax Binding (Q3/2011) and everything works great, except of Export to Excel:
When I follow your instructions on this KB-Article, I am not able to get the data representing the current grid state in the ActionResult Export within my controller class. I'm using an EF Code First ORM together with MVC ViewModels for Data Access. As I'm quite new to EF/MVC I seem to do something wrong!
Excerpt of index.cshtml
Excerpt of UserController.cs
UserIndexViewModel.cs
In the ActionResult Export, in the line "GridModel model = Model().ToGridModel...", I would write "GridModel model = ViewModels.User.UserIndexViewModel.ToGridModel...". But then the symbol ToGridModel cannot be resolved.
Any Idea what I'm doing wrong?
When I follow your instructions on this KB-Article, I am not able to get the data representing the current grid state in the ActionResult Export within my controller class. I'm using an EF Code First ORM together with MVC ViewModels for Data Access. As I'm quite new to EF/MVC I seem to do something wrong!
Excerpt of index.cshtml
@Html.ActionLink("Export to Excel", "Export", new { page = 1, orderBy = "~", filter = "~"}, new { id = "exportLink" }) <script type="text/javascript"> function onDataBound() { var grid = $(this).data('tGrid'); var $exportLink = $('#exportLink'); var href = $exportLink.attr('href'); href = href.replace(/page=([^&]*)/, 'page=' + grid.currentPage); href = href.replace(/orderBy=([^&]*)/, 'orderBy=' + (grid.orderBy || '~')); href = href.replace(/filter=(.*)/, 'filter=' + (grid.filterBy || '~')); $exportLink.attr('href', href); }</script>@( Html.Telerik().Grid<ViewModels.User.UserIndexViewModel>() .Name("User") .TableHtmlAttributes(new { style = "width:" + (config["GridWidth"] > 0 ? config["GridWidth"] + "px" : "100%") }) .Columns(columns => { columns.Bound(u => u.PartnerOrganizationName).Filterable(false).Width(config["PartnerOrganizationNameWidth"]); columns.Bound(u => u.ClownTeamName).Filterable(false).Width(config["ClownTeamNameWidth"]); columns.Bound(u => u.FirstName).Filterable(false).Width(config["FirstNameWidth"]); columns.Bound(u => u.LastName).Filterable(false).Width("100%"); columns.Bound(u => u.PartnerOrganizationId).Visible(false); columns.Bound(u => u.ClownTeamId).Visible(false); columns.Bound(u => u.UserId).Visible(false); }) .DataBinding(dataBinding => dataBinding.Ajax() .Select("_Select", "User"))Excerpt of UserController.cs
public class UserController : Controller{ private readonly ClownOfficeDbContext _db = new ClownOfficeDbContext(); public ActionResult Index() { var model = _db.Users.Select(u => new ViewModels.User.UserIndexViewModel { UserId = u.UserId, ClownTeamId = u.ClownTeamId, ClownTeamName = u.ClownTeam.Name, FirstName = u.FirstName, LastName = u.LastName, PartnerOrganizationId = u.PartnerOrganizationId, PartnerOrganizationName = u.PartnerOrganization.Name }); return View(model); } [GridAction] publicActionResult _Select(GridCommand command) { var model = from u in _db.Users select new ViewModels.User.UserIndexViewModel { UserId = u.UserId, ClownTeamId = u.ClownTeamId, ClownTeamName = u.ClownTeam.Name, FirstName = u.FirstName, LastName = u.LastName, PartnerOrganizationId = u.PartnerOrganizationId, PartnerOrganizationName = u.PartnerOrganization.Name }; return View(new GridModel<ViewModels.User.UserIndexViewModel> { Data = model }); } public ActionResult Export(int page, string orderBy, string filter) { //Get the data representing the current grid state - page, sort and filter GridModel model = Model().ToGridModel(page, 10, orderBy, string.Empty, filter); var orders = model.Data.Cast<ViewModels.User.UserIndexViewModel>(); //Create new Excel workbook var workbook = new HSSFWorkbook();
.... UserIndexViewModel.cs
public class UserIndexViewModel : ViewModelBase { public virtual int UserId { get; set; } public virtual int? ClownTeamId { get; set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } public virtual int? PartnerOrganizationId { get; set; } public virtual string PartnerOrganizationName { get; set; } public virtual string ClownTeamName { get; set; } }}In the ActionResult Export, in the line "GridModel model = Model().ToGridModel...", I would write "GridModel model = ViewModels.User.UserIndexViewModel.ToGridModel...". But then the symbol ToGridModel cannot be resolved.
Any Idea what I'm doing wrong?