Telerik Grid 数据绑定调用 HttpPost 方法

发布于 2024-11-01 18:32:38 字数 4782 浏览 4 评论 0原文

我正在开发 ASP.NET MVC,并且有一个在 HttpPost 操作中检索表单数据的视图。当调用 HttpPost 操作时它工作正常。我使用表单数据查询数据库,并将数据库的结果绑定到 TELERIK 网格控件。它可以正常显示数据,但分页不起作用。

问题是,当我尝试切换到另一个页面时,它调用 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);
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱绻入梦 2024-11-08 18:32:38

首先,您的控制器方法需要 GridAction 属性。其次,它必须使用 GridModel 类型作为模型。这是 ajax 绑定所必需的。您可以检查 ajax绑定帮助文章 以及 ajax 绑定在线演示,其中显示了需要什么步骤 是。最有可能的是,您应该为 ajax 绑定定义一个单独的操作方法。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文