英文:
what is this return statement doing
问题
在下面的代码示例中,return语句使用了一些我觉得奇怪的语法。它返回了一个新的Json结果,但是用作参数传递的对象进行了初始化。有人可以解释一下这个return语句吗?
[AcceptVerbs("Post")]
public ActionResult EditingInline_Destroy([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
{
if (product != null)
{
productService.Destroy(product);
}
return Json(new[] { product }.ToDataSourceResult(request, ModelState));
}
通常情况下,我返回Json数据会像这样做:
// GET: api/authors
[HttpGet]
public JsonResult Get()
{
return Json(_authorRepository.List());
}
英文:
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());
}
答案1
得分: 2
以下是翻译好的部分:
我相信首先创建一个数组,并用产品填充它。然后使用 ToDataSourceResult 方法将其转换为 Json。
"ToDataSourceResult" 似乎是与 Telerik 的 Kendo UI Grid 一起使用的方法,用于从 JSON 显示数据。
英文:
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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论