Asp.net mvc 3 将数据从c linq对象检索到json到html
从json检索代码 代码:-Asp.net mvc 3 将数据从c linq对象检索到json到html,asp.net-mvc-3,getjson,Asp.net Mvc 3,Getjson,从json检索代码 代码:- var collection = getsortcat.Select(x => new { idterm = x.IDTerm, mvo = x.MVO, pic = x.Pic, said = x.SAid, termactive = x.TermActive, vid = x.Vid,
var collection = getsortcat.Select(x => new
{
idterm = x.IDTerm,
mvo = x.MVO,
pic = x.Pic,
said = x.SAid,
termactive = x.TermActive,
vid = x.Vid,
fvo = x.FVO,
eterm = x.ETerm,
edef = x.EDef,
buse = x.BUse,
bterm = x.BTerm,
idcat = x.TermCat,
items = x.TermCategory1.IDCat,
catname = x.TermCategory1.TermCategory1
});
JavaScriptSerializer jss = new JavaScriptSerializer();
string output = jss.Serialize(collection);
return Json(output, JsonRequestBehavior.AllowGet);
Javascript代码:-
success: function (e) {
var txt = "'{ data :" + e + "}'";
var obj = eval("(" + txt + ")");
$('#pdata').append(obj.data[0]);
},
没有得到输出。请给我一个解决方案,如何将数据从c linq对象检索到json到html?首先修复控制器操作,以摆脱任何JavaScriptSerializer和手动管道代码。直接将集合返回到Json结果:
var collection = getsortcat.Select(x => new
{
idterm = x.IDTerm,
mvo = x.MVO,
pic = x.Pic,
said = x.SAid,
termactive = x.TermActive,
vid = x.Vid,
fvo = x.FVO,
eterm = x.ETerm,
edef = x.EDef,
buse = x.BUse,
bterm = x.BTerm,
idcat = x.TermCat,
items = x.TermCategory1.IDCat,
catname = x.TermCategory1.TermCategory1
});
return Json(collection, JsonRequestBehavior.AllowGet);
现在,在success回调中,e参数已经表示解析的对象数组。你不需要打电话给任何评估。通过索引直接访问元素,然后访问属性:
success: function (e) {
var txt = e[0].mvo;
},
您还可以通过以下元素进行循环:
success: function (e) {
for (var i = 0; i < e.length; i++) {
var element = e[i];
alert(element.idterm);
}
},
谢谢,达廷先生。非常感谢你。