首先,我这里使用的是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>
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]
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()~