In a MVC4 app my controller does the following:
public ActionResult MyPage()
{
var myObjects = _db.Object.ToList(); // _db is database context, object consists
// of Id and Name
return View("MyView",
Json(new
System.Web.Script.Serialization.JavaScriptSerializer().Serialize(myObjects )));
}
Now I want to access this data and put in in the body of a table without making another call to the server, like for example the following:
$(function () {
var result = JSON.parse(myObjects);
for (var i = 0; i < result.length; i++) {
renderMyObject(result[i]);
}
function renderMyObject(o) {
var row = document.createElement("tr");
var tdId= document.createElement("td");
tdId.innerHTML = o.Id;
var tdName = document.createElement("td");
tdName.innerHTML = o.Name;
row.appendChild(tdId);
row.appendChild(tdName );
tbody.appendChild(row);
};
});
This does not work since result
(the Model send to the view) is null. Is the above possible and if so how?