I an using json.net to serialize my DataTable. I pass this json string (the result) to the view to render it in an unorder list. But json format is messed up in the 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 my data is messed up like this:
[
{
"
c
o
d
e
"
:
0
,
"
n
a
m
e
"
:
"
A"
}
,
{
...
}
I expect to be:
code: 0, name: A
code: 1, name: B
...
Do I need to deserialize the json object first before looping?
More information: my 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();
}
}
@foreach (var p in Model.tblClinic.clinic){ <li>@p</li> }<li>adds a line break after each character you are iterating though. The foreach has no idea you want to deserialize a JSON string.