c# - my data is messed up when using json.net to serialize DataTable -


i using json.net serialize datatable. pass json string (the result) view render in unorder list. json format messed in result

dataaccess

public static string getclinic() {     string sproc = "getclinic";     return callprocedure(sproc); }  public static string callprocedure(string sproc) {     datatable ds = null;      try     {         using (sqlconnection sqlconn = new sqlconnection(sqlconnstring))         {             sqlcommand cmd = new sqlcommand(sproc, sqlconn);             cmd.commandtype = commandtype.storedprocedure;             sqlconn.open();              sqldataadapter adapter = new sqldataadapter(cmd);              ds = new datatable();             adapter.fill(ds);              string json = jsonconvert.serializeobject(ds);             return json;         }     }     catch (exception ex)     {         throw ex;     } } 

controller

public actionresult index() {      querymodel qmodel = new querymodel {         tblclinic = new clinic(),      };     return view("index", qmodel); } 

view

<ul>     @foreach (var p in model.tblclinic.clinic){         <li>@p</li>     } </ul> 

but data messed this:

[ { " c o d e " : 0 , " n m e " : " a" } , { ... } 

i expect be:

code: 0, name: code: 1, name: b 

...

do need deserialize json object first before looping?

more information: model

public class querymodel {     public clinic tblclinic { get; set; }  } 

my domain

public class clinic {     public string clinic { get; set; }     public clinic() {         this.clinic = data.getclinic();            } } 

do need deserialize json object first before looping?

yes, obviously. need deserialize json object first before looping. otherwise, looping through string produce char @ each step.

deserialization in class may like:

public class rootobject // have set class name {     public int code { get; set; }     public string name { get; set; } } 

then in view, can loop below:

<ul>     @foreach (rootobject p in jsonconvert.deserializeobject<list<rootobject>>(model.tblclinic.clinic))     {         <li>code: @p.code, name: @p.name</li>     } </ul> 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -