简体   繁体   English

如何将对象发送到Telerik MVC Grid Ajax Select()控制器方法

[英]How to send an object to the Telerik MVC Grid Ajax Select() Controller Method

I am using the Telerik MVC Grid with Ajax binding, and am having a problem passing an object to the controller to be used to filter data. 我使用带有Ajax绑定的Telerik MVC Grid,并且在将对象传递给控制器​​以用于过滤数据时遇到问题。 I am able to pass simple data (string, int), but not a more complex object. 我能够传递简单数据(string,int),但不能传递更复杂的对象。

For instance, I can to this no problem: 例如,我可以这样没问题:

.DataBinding(dataBinding => dataBinding.Ajax().Select("_CasesAjaxBinding", "Home", new {orderId = "12345"} ))

And then in my controller, handle the orderId like this: 然后在我的控制器中,像这样处理orderId:

public ActionResult _CasesAjaxBinding(string orderId)

The problem I am having is when I try to pass a more complex object (in this case the @Model) to the controller, like this (The @Model is type CaseFilterModel): 我遇到的问题是当我尝试将更复杂的对象(在本例中为@Model)传递给控制器​​时,就像这样(@Model是类型CaseFilterModel):

.DataBinding(dataBinding => dataBinding.Ajax().Select("_CasesAjaxBinding", "Home", new {filterSpec = @Model} ))

And then trying to handle the object, like this: 然后尝试处理对象,如下所示:

public ActionResult _CasesAjaxBinding(CaseFilterModel filterSpec)

The filterSpec parameter is always null. filterSpec参数始终为null。

Thanks in advance for any ideas! 提前感谢任何想法!

As far as I can find on the Telerik forums this cannot be done this way. 据我所知,在Telerik论坛上,这不可能做到这一点。 There was a similar question on there, which described exactly the same problem. 那里有一个类似的问题,它描述了完全相同的问题。 When passing the model it was always null in the controller's action method. 传递模型时,它在控制器的action方法中始终为null。

However there is a workaround if you want to pass multiple parameters to the select method in order to filter the data, but it requires some client side coding. 但是,如果要将多个参数传递给select方法以过滤数据,则需要一种解决方法,但它需要一些客户端编码。

I'll include a summary of that work around here, so that the answer is complete. 我将在此处包含该工作的摘要,以便答案完整。 A solemn link doesn't say much. 庄严的联系并没有多说。

Assume we have a grid which displays orders items (articles) from all the orders. 假设我们有一个网格,显示所有订单中的订单商品(商品)。 First make sure to hook up the client side onDataBinding event: 首先确保连接客户端onDataBinding事件:

<%= Html.Telerik().Grid<Order>()
        .Name("Grid")
        .ClientEvents(events => events.OnDataBinding("onDataBinding"))
        .DataBinding(dataBinding => dataBinding.Ajax()
                                               .Select("_AjaxBinding", "Grid"))
%>

In the client side event handler you need to compose your select URL. 在客户端事件处理程序中,您需要编写选择的URL。 Here I'll pass two parameters, an order id (int) and a description of an article (string). 在这里,我将传递两个参数,一个订单ID(int)和一篇文章的描述(字符串)。

<script type="text/javascript">

    function onDataBinding(e) {
        var orderId = 100;
        var searchText = "test";
        var params = { OrderId: orderId, ArticleDescription: searchText };
        var paramsStr = $.param(params);
        var selectUrl = "<%= @Url.Action("_AjaxFilterBinding", "Grid") %>" 
            + "?" + paramsStr;
        var grid = $('#Grid').data('tGrid');
        grid.ajax.selectUrl = selectUrl;
    } 

</script>

Then in your controller you can declare the select method as follows: 然后在您的控制器中,您可以声明select方法,如下所示:

[GridAction]
public ActionResult _AjaxFilterBinding(AjaxFilterBindingModel model)
{
    // Retrieve data here and filter it based upon the data present in the model.
    var data = ...;

    return View(new GridModel<Order> { Data = data });
}

The model looks like: 该模型看起来像:

public class AjaxFilterBindingModel
{
    public int OrderId { get; set; }
    public string ArticleDescription { get; set; }
}

Passing a collection via the URL (GET) is also possible. 也可以通过URL(GET)传递集合。 Let's assume you want a collection of order IDs instead of just one. 假设您需要一组订单ID而不是一个订单ID。

The model would look like this: 该模型如下所示:

public class AjaxFilterBindingModel
{
    public IEnumerable<int> OrderIds { get; set; }
    public string ArticleDescription { get; set; }
}

And the JavaScript would look like this: JavaScript看起来像这样:

    function onDataBinding(e) {
        jQuery.ajaxSettings.traditional = true;
        var intArray = [1, 2, 3, 4, 5];
        var params = {  OrderIds: intArray, ArticleDescription: "Test" };
        var paramsStr = $.param(params);
        var selectUrl = "<%= @Url.Action("_AjaxFilterBinding", "Home") %>" + "?" 
            + paramsStr;
        var grid = $('#Grid').data('tGrid');
        grid.ajax.selectUrl = selectUrl;
    }

Remark : Don't forget to set "jQuery.ajaxSettings.traditional = true;" 备注 :不要忘记设置“jQuery.ajaxSettings.traditional = true;” or the parameters will be serialized incorrectly and the ASP.NET MVC model binder will not be able to bind the integer array. 或者参数将被错误地序列化,并且ASP.NET MVC模型绑定器将无法绑定整数数组。

And to be complete here is the Telerik forum thread I mentioned: 在这里完成的是我提到的Telerik论坛帖子:

http://www.telerik.com/community/forums/aspnet-mvc/grid/getting-the-model-object-in-a-custom-binding-grid-ajax-controller.aspx http://www.telerik.com/community/forums/aspnet-mvc/grid/getting-the-model-object-in-a-custom-binding-grid-ajax-controller.aspx

And the suggested work around: 建议的工作:

http://www.telerik.com/community/forums/aspnet-mvc/grid/code-sample-sending-additional-filters-with-ajax-binding.aspx http://www.telerik.com/community/forums/aspnet-mvc/grid/code-sample-sending-additional-filters-with-ajax-binding.aspx

Also as a note, this solution is not in Razor syntax... took me forever to spot it but the line 另外作为一个注释,这个解决方案不是Razor语法...让我永远发现它但是线条

var selectUrl = "<%= @Url.Action("_AjaxFilterBinding", "Home") %>" + "?" var selectUrl =“<%= @ Url.Action(”_ AjaxFilterBinding“,”Home“)%>”+“?” + paramsStr; + paramsStr;

should be changed to 应改为
var selectUrl = "@Url.Action("_AjaxFilterBinding", "Home")" + "?" var selectUrl =“@ Url.Action(”_ AjaxFilterBinding“,”Home“)”+“?” + paramsStr; + paramsStr;

For Razor... I copied the code but couldn't figure out why my _AjaxFilterBinding was not being called. 对于Razor ...我复制了代码,但无法弄清楚为什么我的_AjaxFilterBinding没有被调用。 Just thought I'd point it out in case anyone else has this problem. 只是想我会指出它以防其他人有这个问题。

Thanks for the solution, it NOW works great :) 感谢您的解决方案,它现在很棒:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM