0

我正在使用 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}
4

1 回答 1

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(只是我的偏好)

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

于 2015-09-04T13:34:14.087 回答