1.使用ajax将前台数据传递到后台(将字符串messName传递到后台,接收返回的json数据,并进行处理)
function getWarmMessages() {
$.ajax({
type: "post",
url: "Warning/Detailed",
data: {
messName: messName
},
success: function (result) {
var jsonResult = JSON.parse(result);
alert(jsonResult[0].id);
}
});
}
2.WarningController.cs中新建Detailed方法。(访问数据库,将获取到的数据转换为json格式返回)
public JsonResult Detailed(string messName)
{
var bwr = from b in db.BSB_Warm_Records
where b.StationName == messName
select b;
var bwrList = bwr.ToList();
DataContractJsonSerializer json = new DataContractJsonSerializer(bwrList.GetType());
string szJson = "";
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, bwrList);
szJson = Encoding.UTF8.GetString(stream.ToArray());
}
return Json(szJson);
}