Telerik Grid数据绑定调用HttpPost方法

时间:2011-04-17 20:07:11

标签: asp.net-mvc telerik telerik-grid

我正在使用ASP.NET MVC,我有一个View,在HttpPost操作中检索哪些表单数据。调用HttpPost操作时,它工作正常。我使用表单数据来查询数据库,并将数据库中的结果绑定到TELERIK Grid控件。它显示数据正常,但分页不起作用。

问题在于,当我尝试切换到另一个页面时,它会调用HTTPGET操作方法而不是HttpPost操作方法,因此不会从数据库中检索数据。

感谢任何帮助。

以下是视图和控制器的代码:

//-------------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;
                    dealerProfileViewModel.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);
    }

1 个答案:

答案 0 :(得分:0)

首先,您的控制器方法需要GridAction属性。其次,它必须使用GridModel类型作为模型。这是ajax绑定所必需的。您可以查看ajax binding help article以及显示所需步骤的ajax binding online demo。很可能你应该为ajax绑定定义一个单独的动作方法。

最后,似乎网格的JavaScript没有启动 - 它正在发出HTTP GET请求表明这一点。检查在网格之后是否声明了ScriptRegistrar组件。