开发者

Telerik Grid databinding to call HttpPost method

I am working on ASP.NET MVC and I have a View on which form data is retrieved in the HttpPost action. It works fine when the HttpPost action is called. I use the form data to query a database and the result from the database is bound to a TELERIK Grid control. It displays data fine, but paging is not working.

The issue is that when I try to switch to another page, it calls the HTTPGET action method and not the HttpPost action method and hence no data is retrieved from the database.

Any help is appreciated.

Here is the code for the View and Controller:

//-------------View------------------------------------

@(Html.Telerik().Grid(Model)
            .Name("Grid")
            .Columns(columns =>
            {
                columns.Bound(o => o.DealerName);
                columns.Bound(o => o.DealerNumber);
                columns.Bound(o => o.ServiceDealerNumber);
                columns.Bound(o => o.CMDealerNumber);
                columns.Bound(o => o.PurchaseDealerNumber);
                columns.Bound(o => o.Address);
                columns.Bound(o => o.City);
                columns.Bound(o => o.State);
                columns.Bound(o => o.Zip);
            })
            .DataBinding(dataBinding =>
            {
                dataBinding.Server().Select("DealerProfile", "DealerManagement", new { testVal = "test" }).Enabled(true);
                dataBinding.Ajax().Select("DealerProfile", "DealerManagement", new { testVal = "test" } ).Enabled(true);
            })
            .Scrollable(scrolling => scrolling.Enabled(true))
            .Sortable(sorting => sorting.Enabled(true))
            .Pageable(paging =>
                paging.PageSize(20)
                      .Style(GridPagerStyles.NextPreviousAndNumeric)
                      .Position(GridPagerPosition.Bottom)
            )
            .Filterable(filtering => filtering.Enabled(true))
            .Groupable(grouping => grouping.Enabled(true))
            .Footer(true)
            )

//---------------Controller Actions---------------------------------------
//
    // GET: /DealerManagement/DealerProfile/
    public ActionResult DealerProfile()
    {
        return View();
    }

    //
    // POST: /DealerManagement/DealerProfile/
    [HttpPost]
    public ActionResult DealerProfile(FormCollection formValues)
    {
        string dealerNumber = Request.Form["DealerNumber"];
        string dealerName = Request.Form["DealerName"];
        DealerProfilesViewModel dealerProfilesViewModel = new         DealerProfilesViewModel();
        dealerProfilesViewModel.DealerProfiles = new List<DealerProfileViewModel>();

        if (!dealerNumber.Trim().Equals(string.Empty))
        {
            DealerInfoCollection dealers = _iDealerProfileService.GetDealerInfoFromDealerNumber(dealerNumber);

            foreach (var item in dealers)
            {
                DealerProfileViewModel dealerProfileViewModel = new DealerProfileViewModel();

                dealerProfileViewModel.DealerName = item.Dealer_Name;
                dealerProfileViewModel.DealerNumber = item.Dealer_No;
                dealerProfileViewModel.ServiceDealerNumber = item.Service_Dealer_No;
                dealerProfileViewModel.CMDealerNumber = item.CM_Dealer_No;
                dealerProfileViewModel.PurchaseDealerNumber = item.PUR_Dealer_No;
                dealerProfileViewModel.Address = item.Address;
                dealerProfileViewModel.City = item.City;
                dealerProfileViewModel.State = item.State;
                dealerProfileViewModel.Zip = item.Zip;

                dealerProfilesViewModel.DealerProfiles.Add(dealerProfileViewModel);
            }
        }
        else if (!dealerName.Trim().Equals(string.Empty))
        {
                DealerInfoCollection dealers = _iDealerProfileService.GetDealerInfoFromDealerName(dealerName);

                foreach (var item in dealers)
                {
                    DealerProfileViewModel dealerProfileViewModel = new DealerProfileViewModel();

                    dealerProfileViewModel.DealerName = item.Dealer_Name;
                    dealerProfileViewModel.DealerNumber = item.Dealer_No;
                    dealerProfileViewModel.ServiceDealerNumber = item.Service_Dealer_No;
                    dealerProfileViewModel.CMDealerNumber = item.CM_Dealer_No;
                    dealerProfileViewModel.PurchaseDealerNumber = item.PUR_Dealer_No;
                    dealerProfileVi开发者_如何学运维ewModel.Address = item.Address;
                    dealerProfileViewModel.City = item.City;
                    dealerProfileViewModel.State = item.State;
                    dealerProfileViewModel.Zip = item.Zip;

                    dealerProfilesViewModel.DealerProfiles.Add(dealerProfileViewModel);
                }
        }

        if (!String.IsNullOrEmpty(dealerName) && !String.IsNullOrEmpty(dealerNumber))
        {
            dealerProfilesViewModel = null;
        }

        return View(dealerProfilesViewModel.DealerProfiles);
    }


First your controller method needs the GridAction attribute. Second it must use the GridModel type as a model. This is required for ajax binding. You can check the ajax binding help article as well as the ajax binding online demo which show what the required steps are. Most probably you should define a separate action method just for the ajax binding.

Lastly it seems that the JavaScript of the grid does not kick in - the fact that it is making HTTP GET requests indicate that. Check that there is a ScriptRegistrar component declared AFTER the grid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜