开发人员工具网络标签中的json字符串是:
"{\"1\":\"GJ_CITY1\",\"2\":\"GJ_CITY2\",\"3\":\"GJ_CITY3\",
\"4\":\"GJ_CITY1\",\"5\":\"Aman\",\"6\":\"Aman\",
\"7\":\"Aman\",\"8\":\"Aman\",\"9\":\"Alwar\"}"
我看过各种文章,但这不起作用,我是ajax数据bindig的新手,我的mvc代码是
$.ajax({
type:"GET",
datatype:"JSON",
url: queryLink,
async: true,
success: function (result) {
$('#Cities').html("");//clear before bind
$.each(result, function (i, Cities) {
$("#Cities").append($('<option value=\"" + key + "\">" + val + "</option>'));
});
}
});
我的控制器代码是
CitiesDao cities_obj = new CitiesDao();
HybridNetworkHandoverdto obj = new HybridNetworkHandoverdto();
var query1 = cities_obj.Details().Tables[0].AsEnumerable().Select(t => new CityDto
{
State_id = t.Field<long>("state_id"),
State_name = t.Field<string>("state_name"),
City_id = t.Field<long>("City_id"),
City_name = t.Field<string>("City_name")
}).ToList();
ViewBag.cities = query1;
Dictionary<string, string> abc = new Dictionary<string, string>();
foreach (var item in query1)
{
abc.Add(item.City_id.ToString(),item.City_name);
}
//string jsonResult = JsonConvert.SerializeObject(abc);
var jsonResult = new JavaScriptSerializer();
var json = jsonResult.Serialize(abc);
return Json(json, JsonRequestBehavior.AllowGet);
答案 0 :(得分:2)
执行以下操作 -
$.each(result, function (i, value) {
var html='<option value="'+i+'">' + value + '</option>'
$("#Cities").append(html);
});
答案 1 :(得分:1)
$.each(result, function (i, value) {
var html='<option value="'+i+'">' + value + '</option>'
$("#Cities").append(html);
});
这有效但我必须使用JSON.parse()作为@MKA建议我不知道为什么它适用于Json.parse()
我正在使用斯蒂芬斯回答顺便说一句。