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

using grid with asp.net mvc

3 Answers 355 Views
Grid
This is a migrated thread and some comments may be shown as answers.
realwil
Top achievements
Rank 1
realwil asked on 12 Jan 2012, 06:53 AM
Hello,

I am following Hierarchy example to build the parent-child grid.                  

I have this in the view
function detailInit(e) {
    $("<div/>").kendoGrid({
        dataSource: {
            transport: {
                read:  {
                    url: "../OrderDetailJson/",
                    dataType: "json"
                },
            },
            filter: { field: "orderid", operator: "eq", value: e.data.Id }
        },
     ....
}
 
and this in controller
public JsonResult OrderDetailJson(int orderid){
...
}

but client side code will never hit this code saying orderid is null hence route doesn't match.  Why is filter: { field: "orderid", operator: "eq", value: e.data.Id }  doesn't work???  I thought it will work like jquery .post/json call but it doesn't.

Will someone help me?

thanks guys
william

3 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 12 Jan 2012, 01:27 PM
Hi,

As dataSource is filtered the parameters send to the server will contains the entire filter expression not just the value. Therefore, you should declare the appropriate type which to be mapped by the MVC. However, note that as the MVC binder require JSON to be in specific format you will need to use transport's parameterMap to remap the filter expression similar to the following:

 

var element = $("#grid").kendoGrid({
     dataSource: {               
         transport: {
             read: {
                      url: MYURL,
                      contentType: "application/json; charset=utf-8"
             },
             parameterMap: function(options) {                       
                 var result = {};
                 if (options.filter) {
                     result["filter.logic"] = options.filter.logic;
                     var filters = options.filter.filters;
 
                     for (var idx = 0, length = filters.length; idx < length; idx++) {                               
                         result["filter.filters[" + idx + "].operator"] = filters[idx].operator;
                         result["filter.filters[" + idx + "].field"] = filters[idx].field;
                         result["filter.filters[" + idx + "].value"] = filters[idx].value;                               
                     }                          
                 }                       
                 return result;
             }
         },                               
         serverFiltering: true,
         filter: { field: "orderid", operator: "eq", value: "2" }
     },
     height: 450           

 
});


Then on the server you should have some object structure similar to the following:

public class GridCommand
{
    public FilterExpression Filter { get; set; }       
}
 
public class FilterExpression
{
    public string Logic { get; set; }
    public IEnumerable<GridFilter> Filters { get; set; }
}
 
public class GridFilter
{
    public string  Operator { get; set; }
    public string Field { get; set; }
    public string Value { get; set; }
}

And the Action:

public ActionResult Read(GridCommand command)
 {
            // ...
 }

Regards,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
realwil
Top achievements
Rank 1
answered on 13 Jan 2012, 02:48 AM
I did just that but still getting same error like so

"

The parameters dictionary contains a null entry for parameter 'orderId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult OrderDetailJson(Int32)' in 'Controllers.OrderController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Parameter name: parameters

"

Is there demo project I can look at?  thanks much
0
Rosen
Telerik team
answered on 13 Jan 2012, 09:42 AM
Hi,

I have attached a small sample which demonstrates the basic serialization of DataSource's filter expressions. 

Regards,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
realwil
Top achievements
Rank 1
Answers by
Rosen
Telerik team
realwil
Top achievements
Rank 1
Share this question
or