【问题标题】:Kendo UI Grid returning json instead of displaying dataKendo UI Grid 返回 json 而不是显示数据
【发布时间】:2015-09-08 20:23:38
【问题描述】:

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

【问题讨论】:

    标签: kendo-grid kendo-ui-mvc


    【解决方案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(这里只是我的偏好)

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

    【讨论】:

    • 感谢您的回复。我进行了更改,它仍然返回 json。还有其他想法吗?吉姆
    • 那么你调用什么视图来让这个网格进入浏览器?
    • 视图的名称是_ideasForApproval.cshtml
    • 那么你在 url 中调用了什么?通常按照约定 _xxxx.cshtml 文件用于部分视图。
    • 是的,该视图是局部视图。获取json时浏览器中的URL值为localhost:55943/DashBoard/GetIdeasForApproval
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    相关资源
    最近更新 更多