PDA

View Full Version : C# Data exchange using JSON



CoosPoland
20 May 2017, 3:58 AM
HI,

I'm using Ext JS 6.2 with a C# backend and trying to get data from my database in a grid. So far I got the entity framework setting up a database connection and using Linq I got my data. After parsing the result with javascriptserializer I got the json result to send to my ext frontend. It all works, the ajax call returns the data to the store, but the grid stays blank.

my c# controller

public ActionResult Index()
{
var context = new Database_Entities();
var table = (from Table in context.Table select Table).Take(1);
return Json(new JavaScriptSerializer().Serialize(new { Table = table }), JsonRequestBehavior.AllowGet);
}

the result

"{"Table":[{"ID":1,"strType":"ALL","lngParentID":null,"strHierarchy":"\\\\1\\\","lngExpanded":null,"lngIsFolder":null,"lngLevel":0,"strFullHierarchy":"\\\\ALL\\\","lngChildCount":8,"lngDurationPossible":1}"

the store

Ext.define('ExtExampleApplication.store.Table', {
extend: 'Ext.data.Store',
alias: 'store.table',
model: 'ExtExampleApplication.model.Table',
proxy: {
type: 'ajax',
url: '/Home',
reader: { type: 'json', rootProperty: 'Table'}
},
autoLoad: true
});

It all looks pretty straight forward, but no result in my ext app. What am I missing here? Is my problem the escaped Json and do I need a transform in my store?

Could somebody give me some pointers?

Thanks in advance.

Gary Schlosberg
21 May 2017, 3:04 PM
Any errors or warnings in the console? Your JSON data seems to be incomplete (no closing square and curly brace). Also this may just be how you represented it here, but your JSON is enclosed entirely within double quotes which is not valid.

RobertoRomello
2 Jun 2017, 2:53 AM
Please check for the errors and warnings. You've missed bracket and curly braces... this is why, always consider using a library

CoosPoland
18 Jun 2017, 10:36 PM
Found the solution, just added a transform to decode the data after getting the data by using the Ext.JSON library.

proxy: {
type: 'ajax',
url: '/Home',
reader: {
type: 'json',
rootProperty: '/',
transform: function (data) {
var extdata = Ext.JSON.decode(data);
return extdata;
}
}
},