Kendo UI Grid返回json而不是显示数据

时间:2015-09-04 13:18:02

标签: kendo-grid kendo-ui-mvc

我正在使用Kendo UI MVC网格。控制器正在返回数据,但它没有显示在网格中。我做错了什么?

控制器

 [OutputCache(Duration = 1, VaryByParam = "*")]
    public ActionResult GetIdeasForApproval([DataSourceRequest] DataSourceRequest request)
    {
        IdeaResponse response = this.DashBoardService.GetIdeasForApproval();

        IEnumerable<Idea> ideas = response.Ideas;

        Idea viewModel = new Idea();
        JsonResult result = new JsonResult();

        result.Data =
            Json(
                response.Ideas.Select(
                    p =>
                        new
                        {
                            IdeaId = p.IdeaId,
                            Title = p.Title,
                            Description = p.Description,
                            //Photo = Convert.ToBase64String(p.TeamMember.Photo),
                            URL = p.Url ?? string.Empty
                        }));
        result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

        return result;
    }


@(Html.Kendo().Grid<Idea>()
.Name("ideas-for-approval")
.Columns(columns =>
    {
        columns.Bound(p => p.IdeaId).Visible(false);
        columns.Bound(p => p.Title).Title("Title");
        columns.Bound(p => p.Description).Title("Description");
        columns.Bound(p => p.Url).Title("URL");
    }
)
 .DataSource(dataSource => dataSource
        .Ajax()
        .Events(events => events.Error("onError"))
        .Read(read => read.Action("GetIdeasForApproval", "Dashboard"))
    )

返回了json

{"ContentEncoding":null,"ContentType":null,"Data":[{"IdeaId":431,"Title":"Test","Description":"test","URL":""},{"IdeaId":406,"Title":"Windows 10 For All Developers Test","Description":"Upgrade Windows 7 to Windows 10 for all developers. Test","URL":"https://www.microsoft.com/en-us/windows/features"},{"IdeaId":433,"Title":"Test Title","Description":"Test Description","URL":""}],"JsonRequestBehavior":1,"MaxJsonLength":null,"RecursionLimit":null}

1 个答案:

答案 0 :(得分:1)

接受您的代码并稍微调整一下:

 JsonResult result = new JsonResult();

        result.Data =
            Json(
                response.Ideas.Select(
                    p =>
                        new
                        {
                            IdeaId = p.IdeaId,
                            Title = p.Title,
                            Description = p.Description,
                            //Photo = Convert.ToBase64String(p.TeamMember.Photo),
                            URL = p.Url ?? string.Empty
                        }));
        result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

        return result;

到:

 var model =  response.Ideas.Select(p => new {
                                                    IdeaId = p.IdeaId,
                                                    Title = p.Title,
                                                    Description = p.Description,
                                                    //Photo = Convert.ToBase64String(p.TeamMember.Photo),
                                                    URL = p.Url ?? string.Empty
                                                        });
return Json(model.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);

我个人可能也会将签名更改为公开JsonResult而非ActionResult(仅限我的偏好)

希望这对你有用。如果您需要更多信息,请告诉我,我会为您扩展答案。