Search code examples
c#asp.net-coretelerik

what is this return statement doing


In the code sample below, the return statement uses some syntax that seems strange to me. It's returning a new Json result but initializing it with the object that is passed in as a parameter? Can someone please explain the return statement?

[AcceptVerbs("Post")]
public ActionResult EditingInline_Destroy([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
{            
    if (product != null)
    {                
        productService.Destroy(product);                
    }

    return Json(new[] { product }.ToDataSourceResult(request, ModelState));
}

Returning Json data, I typically do something like this:

// GET: api/authors
[HttpGet]
public JsonResult Get()
{
    return Json(_authorRepository.List());
}

Solution

  • I believe it to first be creating an array which it populates with the product. It then uses the ToDataSourceResult method which converts this to Json.

    The "ToDataSourceResult" seems to be a method used with Teleriks Kendo UI Grid in order to display data from JSON:

    https://doylestowncoder.com/2014/04/14/kendoui-understanding-todatasourceresult/