zoukankan      html  css  js  c++  java
  • MVC3中调用JSON传递对象进行操作

    首先,我这里使用的是MVC3+EF4.1测试环境

     1     <script type="text/javascript">
     2         //created by isaac on 2012-03-27
     3         function PostSave() {
     4             //var j = JSON.parse(data);这个是将返回的数据格式化为数据对象
     5             var product = {
     6                 createDate: null,
     7                 discount: "0.1",
     8                 keepTime: 1,
     9                 price: 1,
    10                 price1: 2,
    11                 proDetails: "testProduct",
    12                 proID: 10000,
    13                 proImage: "testProduct",
    14                 proName: "testProduct",
    15                 proRemark: "testProduct",
    16                 proUnit: "testProduct",
    17                 proUrl: "testProduct",
    18                 typeID: 1
    19             }
    20             var str = Object.serialize2Str(product); //这个是将一个对象封装
    21             //alert(str);
    22             $.ajax({
    23                 type: "POST",
    24                 async: false,
    25                 url: "/Memorial/ProductAdd",
    26                 data: "json=" + str,
    27                 success: function (data) {
    28                     alert(data);
    29                 },
    30                 error: function () {
    31                     alert("Sorry!");
    32                 }
    33             });
    34         }
    35     </script>
    接下来后台Control类去接受
            //Post:/Movies/Add/5
            [HttpPost]
            public JsonResult ProductAdd(string json)
            {
                if (ModelState.IsValid)
                {
                    jdzx_memorial_Product model = JsonHelper.Getjdzx_memorial_Product(json);
                    DataHelper.db.Entry(model).State = EntityState.Added;
                    DataHelper.db.SaveChanges();

                    //return RedirectToAction("ProductList");
                }
                string strJson = "true";
                return Json(strJson);
            }
    [HttpPost]这里你可以根据需要自定义,也可以使用[httpGet]
    此函数中则为response.write()~

  • 相关阅读:
    golang交叉编译:Linux
    vmware共享文件夹
    虚拟机-Debian服务器配置
    day38--MySQL基础二
    day19-IO多路复用
    mysql 对时间的处理
    mysql 优化
    Linux性能查看
    day18-socket 编程
    JAVA 消耗 CPU过高排查方法
  • 原文地址:https://www.cnblogs.com/zhangpan1244/p/2419493.html
Copyright © 2011-2022 走看看