• MVC ajax传递model,ajax接收model


    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);
            } 
       随便找的方法,实验成功了,也就没在继续找更简单了。 
  • 相关阅读:
    JAVA第三周学习总结
    20175303 2018-2019-2 《Java程序设计》第2周学习总结
    20175310 MyCP(课下作业,必做)
    20175310 《Java程序设计》第8周学习总结
    2018-2019-2 20175310 实验二《Java面向对象程序设计》实验报告
    20175310 《Java程序设计》第7周学习总结
    结对编程项目-四则运算 整体总结博客
    20175310 《Java程序设计》第6周学习总结
    20175310结对编程项目-四则运算 阶段总结博客
    20175310 类定义
  • 原文地址:https://www.cnblogs.com/ljh1993/p/4910580.html
Copyright © 2020-2023  润新知