我有列表并从控制器返回,我正在尝试使用 jquery 每个循环函数在 mvc 视图中显示。我可以列出并发送到视图,但是当 jquery 循环启动时我无法获取索引和值。我检查了控制台和源,价值观就在那里。这是我的控制器代码 public JsonResult electric() { int id = Convert.ToInt32(Session["id"]); string cs = "data source=LNPC;initial catalog=db;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"; SqlConnection connection = new SqlConnection(cs); SqlCommand command = new SqlCommand("electrcic_bills", connection); command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@id", id); connection.Open(); SqlDataReader reader = command.ExecuteReader(); List<analiz> TestList = new List<analiz>(); analiz electric; while (reader.Read()) { electric= new analiz(); electric.jan= Convert.ToDouble(reader["jan"].ToString()); electric.feb= Convert.ToDouble(reader["feb"].ToString()); electric.march= Convert.ToDouble(reader["march"].ToString()); electric.april = Convert.ToDouble(reader["april"].ToString()); TestList.Add(electric); } return Json(new { List = TestList }, JsonRequestBehavior.AllowGet); }jQuery代码 $("#electric").click(function () { $("canvas#myCharts").remove(); $("#canvas1").append('<canvas id="myCharts" width="200" height="200"></canvas>'); $.ajax({ type: "GET", url: "/MainController/electric", dataType: "json", success: function (List) { var data = List.List; $.each(data, function (index, value) { alert(data); }); }, }); });使用这种方法我无法获得价值,但是当我像这样编写electric.push(List.List[0].jan._bills_electric) 时,我可以手动获得价值。这是来自浏览器的我的源代码本地列表:列表:数组(1)0:jan_bills:null jan_bills_electric:135 dec_bills:null dec_bills_electric:60
3 回答
摇曳的蔷薇
TA贡献1793条经验 获得超6个赞
好吧,我找到了我的答案,我哪里错了。首先-我的控制器没有问题其次-在每个循环函数中,我的数组不仅是数组,它还是 OBJECT 中的数组。我找到了这个链接并尝试了每个循环中的每个循环,我从 jquery 循环中获取了我的项目.
var json = [
{ 'red': '#f00' },
{ 'green': '#0f0' },
{ 'blue': '#00f' }
];
$.each(json, function () {
$.each(this, function (name, value) {
console.log(name + '=' + value);
});
});
添加回答
举报
0/150
提交
取消
