c# - Linq to Json MVC 5 not formatting as expected -
might seem simple can't json correctly formatted mvc 5 controller.
my output showing as;
[ { "result":{ "account_color":"b43104", "account_desc":"welcome xyz", "account_name":"xyz", "account_zone":1 } }, { "result":{ "account_color":"ff0000", "account_desc":"test company", "account_name":"test", "account_zone":2 } } ]
so can see level above result not show text seems 'result' section getting added blank node
my controller is;
public ienumerable<dynamic> get() { return db.tblaccounts.select(o => new { result = new { account_name = o.account_name, account_desc = o.account_desc, // account_image = url.content("~/images/") + string.format("account_{0}.png", o.id), account_color = o.account_color, } }); }
my json formatter is;
config.formatters.clear(); //config.formatters.add(new xmlmediatypeformatter()); var json = new jsonmediatypeformatter(); json.serializersettings.preservereferenceshandling = newtonsoft.json.preservereferenceshandling.none; config.formatters.add(json);
so appears 'result' @ sub level needs level higher, great.
try changing code this:
public ienumerable<dynamic> get() { return db.tblaccounts.select(o => new { account_name = o.account_name, account_desc = o.account_desc, // account_image = url.content("~/images/") + string.format("account_{0}.png", o.id), account_color = o.account_color, } ); }
this produce array of results instead of nested json provided. don't need result
node label you?
edit:
based on response looking for?
public dynamic get() { var accountsnode = new { accounts = db.tblaccounts.select(o => new { account_name = o.account_name, account_desc = o.account_desc, // account_image = url.content("~/images/") + string.format("account_{0}.png", o.id), account_color = o.account_color, } ).tolist() }; return accountsnode; }
Comments
Post a Comment