JSON appears to user instead of formatted Grid

2 Answers 61 Views
Grid View
Rick
Top achievements
Rank 1
Iron
Rick asked on 30 Jun 2023, 07:02 PM
I'm using Kendo MVC. The data is appearing as the JSON that returned from the controller instead of a formatted KendoGrid. Even when I remove the contents of the entire CSHTML file, it still shows the JSON to the user. The JSON looks to be correctly formatted. What could cause that?

2 Answers, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 05 Jul 2023, 08:35 AM

Hello Rick,

Could you post the declaration of the Grid and the action that returns the data?

Regards,
Ivan Danchev
Progress Telerik

As of R2 2023, the default icon type will be SVG instead of Font. See this blogpost for more information.

Rick
Top achievements
Rank 1
Iron
commented on 05 Jul 2023, 12:47 PM

The action is as follows:

public ActionResult getMNotes([Kendo.Mvc.UI.DataSourceRequest] Kendo.Mvc.UI.DataSourceRequest request, string m_key)
{
    if (!string.IsNullOrEmpty(m_key))
    {
        var token = ((ClaimsIdentity)User.Identity).FindFirst("token");
        var client_key = ((ClaimsIdentity)User.Identity).FindFirst("client_key");

        // The cshtml shows the Delete and Edit button only when the user is an admin
        var role = ((ClaimsIdentity)User.Identity).FindFirst(ClaimTypes.Role).Value.ToString();
        ViewData["role"] = role;
        mLookUpHTTP http = new mLookUpHTTP();

        System.Web.Script.Serialization.JavaScriptSerializer r = new System.Web.Script.Serialization.JavaScriptSerializer();
        var jsonObj = r.Deserialize<dynamic[]>(http.getMNotes(token.Value, client_key.Value, m_key)).ToList().ToDataSourceResult(request);
        return Json(jsonObj, JsonRequestBehavior.AllowGet);
    }
    return Json(new { Status = "Error", Message = "No M Passed" }, JsonRequestBehavior.AllowGet);
}

Even when I reduce the entire cshtml page to this, it still shows JSON:

<p>xxxxx</p>
@(Html.Kendo().Grid<patha.pathb.Models.MModel>()
.Name("xxx")
);
Ivan Danchev
Telerik team
commented on 10 Jul 2023, 01:31 PM

Rick,

I've attached a sample project, in which the Grid is bound to data returned by the getMNotes action. Note that I've commented the conditional logic: 

if (!string.IsNullOrEmpty(m_key))

and the missing dependencies and I am returning a sample collection of MModel. I am not sure how m_key is sent to the action. Most likely this is done through the Grid's dataSource Data function. Nevertheless, regardless of returning:

return Json(jsonObj, JsonRequestBehavior.AllowGet);

or:

return Json(new { Status = "Error", Message = "No M Passed" }, JsonRequestBehavior.AllowGet);

in both cases I don't get JSON to appear on the page. In both cases the Grid is rendered and the difference is: in the first case the Grid displays the data, in the second case the Grid is displayed empty, since the response contains the error status. 

Could you please modify the sample project accordingly so that it demonstrates the issue and attach it back for further review?

0
Rick
Top achievements
Rank 1
Iron
answered on 14 Jul 2023, 02:31 PM

Hi Ivan. Thanks for looking into this! This is working now. I believe that the source of the problem was this line of JavaScript in my cshtml:

var url = '@Url.Action("addMeterNote", "Default")

which should have been:

var url = '@Url.Action("addMeterNote", "Default")';

Apparently it was adding some of the subsequent lines of code to the url variable instead of using treating them as code.

Thanks again! - Rick

Ivan Danchev
Telerik team
commented on 19 Jul 2023, 12:58 PM

Rick,

Thank you for the follow up. I am glad you've identified what was causing the issue and it is now fixed.

Regards,
Ivan Danchev
Progress Telerik

Tags
Grid View
Asked by
Rick
Top achievements
Rank 1
Iron
Answers by
Ivan Danchev
Telerik team
Rick
Top achievements
Rank 1
Iron
Share this question
or