简体   繁体   中英

How do I pass an array of parameters to a Kendo Grid Datasource?

I would like to pass an array of data to a Kendo Grid Datasource. The array will be a selection of Id's that return specific cars to the grid, I'm not sure how to do it.

Typically you can pass a single parameter to the grid by either directly declaring it inline or (the way I prefer) leverage .Data and specify a javascript function.

.Read(read => read.Action("GetCars", "Grid").Data("myParams"))

You would then write a function to return your data, in this case a single Id, plus anything else you wanted from a record.

function myParams() {
    return {
        name: "Holden",
        id: 1
    }
}

Then we adjust the controller method to make use of the parameter.

public ActionResult GetCars([DataSourceRequest] DataSourceRequest request, int Id)
{
    var car = unitOfWork.CarRepository.Get()Where(x => x.Id == Id);
    var result = car.ToDataSourceResult(request);
    return Json(result, JsonRequestBehavior.AllowGet);
}

That's all good and well for passing a single Id but I need to pass in an array of them. I started down a path I thought would accomplish this but to be honest I'm not totally convinced. I made a very crude initial approach in javascript, something like:

var cars = ["1", "2", "3"];

function myParams() {
    return {
        car: cars
    }
}

Then I adjusted my controller to accept the array like this:

public ActionResult GetTabVessels(string[] car, [DataSourceRequest] DataSourceRequest request)
{
    // Not sure how to handle it at this stage :(

    foreach (var item in car) {
        Console.WriteLine(item);
    }
    //Need to adjust this to accept the array.  Any ideas?
    var car = unitOfWork.CarRepository.Get()Where(x => x.Id == Id);
    var result = car.ToDataSourceResult(request);
    return Json(result, JsonRequestBehavior.AllowGet);
}

After this point I realised I may need some guidance. So, how do I pass an array of parameters (Id's in my case) to a Kendo Grid Datasource?

Many thanks

As discussed in the comments, here is an untested example of how to return an array to Kendo:

public ActionResult GetTabVessels(string[] car, [DataSourceRequest] DataSourceRequest request)
{
    //ToList() is a Linq Extension
    return Json(car.ToList().ToDataSourceResult(request));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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