这个return语句在做什么?

huangapple go评论82阅读模式
英文:

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/

huangapple
  • 本文由 发表于 2020年1月6日 23:01:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定