当我将以下linq结果传递到我的下拉列表时,我得到“未定义”,这里我有结果(在控制器中),我知道原因可能是在“select new”之后我没有指定任何特定的类但我需要它像这样,
var newlist_of_device_types = (from z in DB.t_device_details
where z.type_id == DB.t_device_type.Where(t => t.device_type == "PLC")
.Select(t => t.type_id)
.FirstOrDefault()
select new
{
model = (z.model + "-" + z.producer),
}).Where(z => z.model != null).ToList();
“查看”
$.ajax({
dataType: "json",
type: "POST",
url: "@Url.Action("deviceTypeList","AdminTool")",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "flg": flg_dvType, }),
success: function (data) {
mydata_deviceType = data;
}
});
我在Ajax中获得的内容的屏幕截图
答案 0 :(得分:0)
你需要从控制器返回json。我看到你正在使用POST(确保你使用get然后你需要允许get - JsonRequestBehavior) 所以这样做:
var newlist_of_device_types = (from z in DB.t_device_details
where z.type_id == DB.t_device_type.Where(t => t.device_type == "PLC")
.Select(t => t.type_id)
.FirstOrDefault()
select new
{
model = (z.model + "-" + z.producer),
}).Where(z => z.model != null).ToList();
return Json(newlist_of_device_types);
你的ajax实际上是正确的:
$.ajax({
url: "@Url.Action("deviceTypeList","AdminTool")",
type: "POST",
data: JSON.stringify(jsonObject),
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (response) {
alert(response.responseText);
},
success: function (response) {
var $select = $('#ID of your dropdown list');
$.each(response, function(i, m) {
console.log(m);
var option = $('<option>', {
value: m.model //since you have no id here
}).html(m.model).appendTo($select);
console.log(option);
});
}
});
希望这会有所帮助!!
也许您可以在浏览器中发布调试控制台的屏幕截图,以便我可以看到数据