This is a migrated thread and some comments may be shown as answers.

[Solved] _CustomBinding Action Method Not Called

7 Answers 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jim
Top achievements
Rank 1
Jim asked on 20 Jul 2011, 07:51 PM
I'm trying to control the paging behavior on the Grid.  I'm trying to accomplish this through Custom Binding.  Not sure if that's right nor not.

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

Sort by
0
Atanas Korchev
Telerik team
answered on 21 Jul 2011, 07:04 AM
Hi Jim,

 You can do so via the DataBinding setting:

.DataBinding(dataBinding => dataBinding.Server().Select("action", "controller"))

However during server binding the whole page will refresh thus you cannot use a partial view - you need to render the whole view again. If you don't want to do that you can implement ajax binding. Here are links to our custom binding examples:

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!

0
Jim
Top achievements
Rank 1
answered on 21 Jul 2011, 12:57 PM
I was following the example at http://demos.telerik.com/aspnet-mvc/grid/custombinding, but the
[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.
0
Atanas Korchev
Telerik team
answered on 21 Jul 2011, 01:20 PM
Hello Jim,

 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();
}

Best wishes,
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!

0
Jim
Top achievements
Rank 1
answered on 21 Jul 2011, 01:44 PM
When the user switches pages in the grid control, only the grid should be refreshed, not the entire page.  If that can only be accomplished through Ajax binding, then I need to be doing Ajax binding.  I have zero experience with either Telerik or Ajax, so it isn't clear to me what I'm doing that makes my use of the grid "server binding."

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

0
Atanas Korchev
Telerik team
answered on 21 Jul 2011, 01:49 PM
Hello 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!

0
Jim
Top achievements
Rank 1
answered on 21 Jul 2011, 03:09 PM
Ya, I was missing the part where I tell it which controller method to call.

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)
               [...]
0
Accepted
Atanas Korchev
Telerik team
answered on 21 Jul 2011, 04:08 PM
Hello Jim,

 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!

Tags
Grid
Asked by
Jim
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Jim
Top achievements
Rank 1
Share this question
or