如何在.NET MVC中使用jQuery并返回JSON数据
展开全部
后台代码:
public JsonResult GetAnYou()
{
var aySql = "select * from T_User";
var ayResult = dbContext.Database.SqlQuery<AY>(aySql).ToList();
var data = ayResult.Select(p => new { MC = p.MC, DM = p.DM, FDM = p.FDM });
return Json(new { data = data }, JsonRequestBehavior.AllowGet);
}
JS代码:
$.ajax({
url: "@Url.Content("~/Document/GetAnYou")",
data: { "ajlb": ajlxVal },
cache: false,
async: false,
success: function (result) {
$.each(result.data, function (index, firstValue) {
if (firstValue.FDM == 0) {
mydtree.add(firstValue.DM, 0, firstValue.MC, '', firstValue.MC, "_self", false);
$.each(result.data, function (index, secondValue) {
if (secondValue.FDM == firstValue.DM) {
mydtree.add(secondValue.DM, firstValue.DM, secondValue.MC, '', secondValue.MC, "_self", false);
$.each(result.data, function (index, thirdValue) {
if (thirdValue.FDM == secondValue.DM) {
mydtree.add(thirdValue.DM, secondValue.DM, thirdValue.MC, "javascript:setvalue('" + thirdValue.DM + "','" + thirdValue.MC + "')", thirdValue.MC, "_self", false);
}
});
}
});
}
});
document.write(mydtree);
}
});
public JsonResult GetAnYou()
{
var aySql = "select * from T_User";
var ayResult = dbContext.Database.SqlQuery<AY>(aySql).ToList();
var data = ayResult.Select(p => new { MC = p.MC, DM = p.DM, FDM = p.FDM });
return Json(new { data = data }, JsonRequestBehavior.AllowGet);
}
JS代码:
$.ajax({
url: "@Url.Content("~/Document/GetAnYou")",
data: { "ajlb": ajlxVal },
cache: false,
async: false,
success: function (result) {
$.each(result.data, function (index, firstValue) {
if (firstValue.FDM == 0) {
mydtree.add(firstValue.DM, 0, firstValue.MC, '', firstValue.MC, "_self", false);
$.each(result.data, function (index, secondValue) {
if (secondValue.FDM == firstValue.DM) {
mydtree.add(secondValue.DM, firstValue.DM, secondValue.MC, '', secondValue.MC, "_self", false);
$.each(result.data, function (index, thirdValue) {
if (thirdValue.FDM == secondValue.DM) {
mydtree.add(thirdValue.DM, secondValue.DM, thirdValue.MC, "javascript:setvalue('" + thirdValue.DM + "','" + thirdValue.MC + "')", thirdValue.MC, "_self", false);
}
});
}
});
}
});
document.write(mydtree);
}
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询