ajax - MVC5 not passing JSON property names through to view -


i'm starting out mvc, json, ajax, etc , side project have been trying create data viz dashboard.

today followed guide on how pass simple table of data sql json view: http://techfunda.com/howto/292/list-records-using-json

it worked: jsonresult comes through controller , contains values not property names. causes problem because i'm referencing property names when process data display in javascript.

here's sql data:

sql data

here's model:

    public partial class vw_dash_chartdata : ienumerable<object>         {             [key]             [jsonproperty(propertyname = "classification")]             public string classification { get; set; }              [jsonproperty(propertyname = "count")]             public int count { get; set; }              public ienumerator<object> getenumerator()             {                 yield return classification;                 yield return count;             }              system.collections.ienumerator system.collections.ienumerable.getenumerator()             {                 return this.getenumerator();             }         } 

(you'll notice tried manually set [jsonproperty(...)] stuff...it didn't help.)

and here's jsonresult:

    public jsonresult chartdatajson()             {                 var data = new list<vw_dash_chartdata>();                 data = db.vw_dash_chartdatas.tolist();                  var jsondata = json(data, jsonrequestbehavior.allowget);                 return jsondata;             } 

(initially sending data straight through dbcontext thought perhaps use vw_dash_chartdata model. didn't make difference).

my view looks following:

    @{         viewbag.title = "charts";         ajaxoptions options = new ajaxoptions         {             //confirm = "are sure?",             loadingelementid = "divloading",             onsuccess = "processdatamethod",             url = url.action("chartdatajson")         };     }      <script type="text/javascript">         function processdatamethod(data) {             var output = $("#datazone");             output.empty();             (var = 0; < data.length; i++) {                 var chartdata = data[i];                 output.append("<tr><td>" + chartdata.classification + "</td><td>" + chartdata.count + "</td></tr>");             }         }     </script>      <div>         <table>             <thead>                 <tr>                     <th>classification</th>                     <th>count</th>                 </tr>             </thead>             <tbody id="datazone">             </tbody>         </table>     </div>      @using (ajax.beginform(options))     {         <div id="divloading" style="color: red; font-size: larger;">              loading...         </div>         <div>             <button type="submit" id="btnclicky" >clicky</button>         </div>     }      <script>         $("#btnclicky").trigger("click");     </script> 

when load page, get:

page result

and json object shown in browser developer tools;

json object

any tips/ideas gratefully received! also, if i'm doing stupid let me know i'd learn best practice stuff.

how about?

var jsondata = json(data.select(x=> new {      classification = x.classification,     count = x.count     }) );  return jsondata; 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -