The problem is, my _CustomBinding() method isn't getting called when I try to move to a new page, and I'm not sure how I tell the grid to call that method?
My controller looks like:
// This is called when the initial search is done:
public ActionResult AcctPgmSearch()
{
return View("Index", DoAcctPgmSearch(1));
}
// This is supposed to be called when the user switches between pages:
[GridAction(EnableCustomBinding = true)]
public ActionResult _CustomBinding(GridCommand command)
{
return PartialView( "AcctPgmSearchResults", DoAcctPgmSearch(command.Page));
}
And the definition of the Grid looks like:
@(Html.Telerik().Grid<CommonModel.AcctProgram.
IAccountingProgram>(Model.Items)
.Name("SearchResultsGrid")
.DataKeys(keys => keys.Add(item => item.Id))
.EnableCustomBinding(true)
.Columns(columns =>
{
columns.Template(pgm => "27").Title("Id");
columns.Bound(pgm => pgm.Column).Title("Reporting Period");
})
.Sortable(sorting => sorting.SortMode(GridSortMode.MultipleColumn))
.Pageable(paging => paging.PageSize(10).Total(Model.TotalNumberOfMatches))
.Filterable(filtering => filtering.Enabled(false))
.Groupable(grouping => grouping.Enabled(false))
.Footer(true))
How do I tell it to call a particular action method on a particular controller? Right now, it's calling the "initial search" method, instead of the _CustomBinding method.
7 Answers, 1 is accepted
You can do so via the DataBinding setting:
.DataBinding(dataBinding => dataBinding.Server().Select("action", "controller"))
http://demos.telerik.com/aspnet-mvc/grid/custombinding
http://demos.telerik.com/aspnet-mvc/grid/customserverbinding
Regards,
Atanas Korchev
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!
[GridAction(EnableCustomBinding = true)]
public ActionResult _CustomBinding(GridCommand command)
method doesn't get called.
I was able to get around part of the problem by using the Request.Params["SearchResultsGrid-page"] info:
public ActionResult AcctPgmSearch()
{
var vm = new AcctPgmSearchViewModel();
vm.SearchCriteria.PageSize = 10;
// Get info that theoretically would be passed as part of the GridCommand
var page = Request.Params["SearchResultsGrid-page"];
vm.SearchCriteria.PageToGet = (page == null) ? 1 : Convert.ToInt32(page);
[ snip: Do the Search ]
// This will happen when the user presses the "Search" button, thus initiating a new search
if(page == null)
return View("Index", vm);
// This will happen when the user changes pages inside the Telerik grid
return PartialView("AcctPgmSearchResults", vm.SearchResults);
}
Unfortunately, when the user changes pages inside the grid, the entire page gets refreshed, and the top half (with the search criteria form on it) disappears, while the bottom half (with the Telerik grid) displays the grid as a table.
The "Search" page for my application looks like this:
@model SearchViewModel
@{
ViewBag.Title = "Search";
}
<h2>Search</h2>
@using (Html.BeginForm("AcctPgmSearch", "Search", FormMethod.Get, new { id = "AcctPgmSearchForm", @class="SpeForm" }))
{
<fieldset>
[ snip ]
<input type="submit" value="Search!" />
</fieldset>
}
@Html.ActionLink("Reset Criteria", "Index")
@if(Model.SearchResults != null)
{
<div>
@Html.Partial("AcctPgmSearchResults", Model.SearchResults)
</div>
}
The "AcctPgmSearchResults" partial page contains the Telerik grid.
The example you are following is for custom ajax binding whereas you seem to be using serve binding. You cannot use a PartialView to bind the grid with server binding as it will return only the partial and replace the whole page (which is actually expected). If you want to implement custom server binding please follow the other example:
http://demos.telerik.com/aspnet-mvc/grid/customserverbinding
Here is the relevant code:
[GridAction(GridName = "Grid")]public ActionResult CustomServerBinding(GridCommand command){ IEnumerable data = GetServerData(!IsEmptyCommand(command) ? command : new GridCommand()); return View(data);} private IEnumerable GetServerData(GridCommand command){ DataLoadOptions loadOptions = new DataLoadOptions(); loadOptions.LoadWith<Order>(o => o.Customer); var dataContext = new NorthwindDataContext { LoadOptions = loadOptions }; IQueryable<Order> data = dataContext.Orders; //Apply filtering data = data.ApplyFiltering(command.FilterDescriptors); ViewData["Total"] = data.Count(); //Apply sorting data = data.ApplySorting(command.GroupDescriptors, command.SortDescriptors); //Apply paging data = data.ApplyPaging(command.Page, command.PageSize); //Apply grouping if (command.GroupDescriptors.Any()) { return data.ApplyGrouping(command.GroupDescriptors); } return data.ToList();}Atanas Korchev
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!
From http://demos.telerik.com/aspnet-mvc/grid/custombinding, it isn't clear to me what else I need to do to get just the grid to refresh. I've tried sending back a PartialViewResult and also doing "return Json(...)". The PartialViewResult breaks, and trying to return Json gives me a security error saying I shouldn't return Json as part of a Get method.
Thanks,
Jim
You can check the ajax binding and custom binding help topics. They should help you get started.
Regards,Atanas Korchev
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!
So now I'm able to call a specific method and only the grid refreshes. Unfortunately, it only ever says "No records to display."
My controller action looks like:
[GridAction]
public ActionResult SearchResultsGridRequest()
{
var viewModel = DoSearch();
var gridModel = new GridModel<IAccountingProgram>
{
Data = viewModel.SearchResults.Items,
Total = viewModel.SearchResults.TotalNumberOfMatches
};
return View(gridModel);
}
When I step through, I see that "Data" contains one page worth of items (10 rows) as expected, and "Total" is set to 226, as expected.
I define the Grid itself like:
@(Html.Telerik().Grid<CommonModel.AcctProgram.IAccountingProgram>(Model.Items)
.Name("SearchResultsGrid")
.DataKeys(keys => keys.Add(item => item.Id))
.DataBinding(dataBinding => dataBinding.Ajax().Select("SearchResultsGridRequest", "Search"))
.EnableCustomBinding(true)
[...]
The GridAction attribute lacks the CustomBinding setting:
[GridAction(EnableCustomBinding = true)]public ActionResult _CustomBinding(GridCommand command){Greetings,
Atanas Korchev
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!