I'm using asp.net mvc2 and trying to send a list of json objects with hard coded values from the home controller, and receive them in index.... in the code below i'm sending 1 json object .... how do i send many?
in home controller:
public ActionResult JsonValue()
{
var result = new
{
pID = 1,
pName = "Lina",
pStart = "",
pEnd = "",
pColor = "ff0000",
pLink = "",
pMile = 0,
pRes = "Brian",
pComp = 0,
pGroup = 1,
pParent = 0,
pOpen = 1
};
return Json(result,JsonRequestBehavior.AllowGet);
}
and receiving it in index like this:
var Jid = null;
var Jname = null;
var Jstart = null;
var Jend = null;
var Jcolor = null;
var Jlink = null;
var Jmile = null;
var Jres = null;
var Jcomp = null;
var Jgroup = null;
var Jparent = null;
var Jopen = null;
var Jtitle = null;
var g = new 开发者_如何学编程JSGantt.GanttChart('g', document.getElementById('GanttChartDIV'), 'day');
$(document).ready(function () {
$.getJSON('../../Home/JsonValue', function (data) {
Jid = data.pID;
Jname = data.pName;
Jstart = data.pStart;
Jend = data.pEnd;
Jcolor = data.pColor;
Jlink = data.pLink;
Jmile = data.pMile;
Jres = data.pRes;
Jcomp = data.pComp;
Jgroup = data.pGroup;
Jparent = data.pParent;
Jopen = data.pOpen;
Jtitle = '|id= ' + Jid + '|Name: ' + Jname + '|Start: ' + Jstart + '|End: ' + Jend;
}); // end $.getJSON
thanks a million in advance... Lina
add them to an array and return that (via the JSON call of course).
personally I would make a class rather than the anonymous object you have and then add to a generic list, once you have the list filled you can pass the list.ToArray() into the Json call. I havent tried but you may be able to pass the list direct to the Json (I am unsure if it will create a Json array from a generic list).
edit,
It looks like Json will turn any enumerable into a Json array so I would say you can pass the list generic list in, I will add some code later
code added below.
I dont for one minute think you should code a List<object>
personally, as explained I would create a class for your anonymous object so you can strongly type it. hopefully this will give you the idea though
public ActionResult JsonValue()
{
List<object> jsonlist = new List<object>();
jsonlist.Add(new
{
pID = 1,
pName = "Lina",
pStart = "",
pEnd = "",
pColor = "ff0000",
pLink = "",
pMile = 0,
pRes = "Brian",
pComp = 0,
pGroup = 1,
pParent = 0,
pOpen = 1
});
jsonlist.Add(new
{
pID = 1,
pName = "Lina",
pStart = "",
pEnd = "",
pColor = "ff0000",
pLink = "",
pMile = 0,
pRes = "Brian",
pComp = 0,
pGroup = 1,
pParent = 0,
pOpen = 1
});
return Json(jsonlist,JsonRequestBehavior.AllowGet);
}
精彩评论