簡體   English   中英

Kendo UI Grid返回json而不是顯示數據

[英]Kendo UI Grid returning json instead of displaying data

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

將您的代碼並從以下方面進行微調:

 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 (僅在此處提供我的偏好)

希望這對您有用。 如果您需要更多信息,請告訴我,我們將為您擴展答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM