Learning Curve
Learning Curve

Reputation: 1569

Kendo MVC Razor Grid How to pass a single model rather collection from the controller

For a normal scenario, Kendo MVC Razor or any other grid supposedly be bound to a collection where controller passes the DataSourceRequest to the DataSourceResult of the collection and return Json result as follow

    public ActionResult GetData([DataSourceRequest] DataSourceRequest request, string taskKey)
    {
        var query = eSerivce.GetData(taskKey);
        var result = query.modelCollection.ToDataSourceResult(request);
        return Json(result, JsonRequestBehavior.AllowGet);
    }

Above code work fine for any kind of collection and grid will render the data fine.

In my scenario, I have a single model returned rather a collection by the service layer and as there is no ToDataSourceResult available on single entity so I can't pass the DataSourceRequest to it and without DataSourceRequest it is not rendering any information.

e.g.

    public ActionResult GetData([DataSourceRequest] DataSourceRequest request, string taskKey)
    {
        var query = eSerivce.GetData(taskKey);
        var result = query.   // no ToDataSourceResult available here because of single model
        return Json(query, JsonRequestBehavior.AllowGet);
    }

In above snippet as there is no ToDataSourceResult available, when I return query, Grid dont render any data. I know I can convert my single model entity to a collection to apply the necessory operation but I am just wondering if there is any other way of work around to this situation. Main reason of returning only one record is to display the excessive information of the parent grid in a custom templated row on a row expand.

Upvotes: 0

Views: 409

Answers (1)

Marcin
Marcin

Reputation: 3262

Kendo DataSource (not only grid) expect array, return model in different way:

if simple list:

return Json(new[] { query }, JsonRequestBehavior.AllowGet);

or if type: "aspnetmvc-ajax"

return Json(new[] { query }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

Upvotes: 1

Related Questions