标签:
MVC ajax传递model,ajax接收model
传递model
var models = []; $.each($("table tr"), function(i, item) { var OilOmeterID = oid;
var OilHeight = oh; var WaterHeight = $("#WaterHeight").val(); var OilVolume = ov; var WaterVolume = $("#WaterVolume").val(); var AllVolume = $("#AllVolume").val(); var OilTemperature = ot; var Density = $("#Density").val(); var ATemperature = $("#ATemperature").val(); var OilQuality = $("#OilQuality").val(); models.push({ OilOmeterID: OilOmeterID, OilHeight: OilHeight, WaterHeight: WaterHeight, OilVolume: OilVolume, WaterVolume: WaterVolume, AllVolume: AllVolume, OilTemperature: OilTemperature, Density: Density, ATemperature: ATemperature, OilQuality: OilQuality }); return false;//跳出循环,如果你要传递的是一个list集合,就不需要写这句话,return false==break. }); $.ajax({ type: "POST", url: "@Url.Action("QueryData", "OilDepotOverview")", data: JSON.stringify(models), contentType: ‘application/json; charset=utf-8‘, success: function (sesponseTest) { alert(sesponseTest); } }); Controller public ActionResult QueryData(List<O_OilCalculate> OilCalculate)//还是要用集合的方式接收 { OilDepotContext db = new OilDepotContext(); O_OilCalculate oc = new O_OilCalculate(); if (OilCalculate.Count>0) { oc = OilCalculate[0];//只要一个对象 db.O_OilCalculates.Delete(o => o.OilOmeterID == oc.OilOmeterID); db.O_OilCalculates.Add(oc); db.SaveChanges(); return Content("保存入库成功。"); } else { return Content("保存数据失败。"); } } 返回model $.ajax({ type: "POST", url: "@Url.Action("GetJson", "OilDepotOverview")", data: { id: id }, success: function (sesponseTest) { if (sesponseTest.OilHeight!=0) { document.getElementById(‘OilHeight‘).value = sesponseTest.OilHeight; } }
}); public JsonResult GetJson(string id) { var Query = from d in db.O_OilCalculates select d; int? oilid = 0; if (id == "") { oilid = 0; } else { oilid = Convert.ToInt32(id); } var result = Query.Where(oo => oo.OilOmeterID == oilid); O_OilCalculate oc = new O_OilCalculate(); if (result.ToList().Count>0) { oc = result.ToList()[0] as O_OilCalculate; } return Json(oc, JsonRequestBehavior.AllowGet); } 随便找的方法,实验成功了,也就没在继续找更简单了。
|
标签:
原文地址:http://www.cnblogs.com/ljh1993/p/4910580.html