0

我将一个 JSON 对象发布到 asp.net mvc 控制器操作,如下所示。控制器操作自动将 json 变量绑定到单个对象。虽然这是一个很好的功能,但我的要求是将传入的 json 字符串“原样”捕获到单个字符串对象中并将其存储在数据库中。我的问题是如何覆盖默认行为并以单个字符串获取传入的 json 对象?

看法

$.ajax({
            type: "POST",
            url: "@(storeLocation)IDR/OpcInsertCustomerProfile/",
            data: JSON.stringify(currSelection),
            contentType: "application/json",
            success: function(data) {
              alert('success : ' + JSON.stringify(data));
            },
            error: function(data) {
             alert('Error : ' + JSON.stringify(data));
            }
          }
        );

控制器

[HttpPost]
        [ActionName("OpcInsertCustomerProfile")]
        public JsonResult OpcInsertCustomerProfile(string jsonProfile)
        {
            try
            {
                JavaScriptSerializer ser = new JavaScriptSerializer();
                return Json(jsonProfile, JsonRequestBehavior.AllowGet);

            }
            catch (Exception exc)
            {
                return Json(new { error = 1, message = exc.Message });
            }
        }
4

2 回答 2

0

您可以尝试类似的方法将您的内容转换回 json

System.Web.Script.Serialization.JavaScriptSerializer oSerializer = 
     new System.Web.Script.Serialization.JavaScriptSerializer();

string sJSON = oSerializer.Serialize(oList);


[{"Name":"Pini","Age":"30","ID":"111"},
{"Name":"Yaniv","Age":"31","ID":"Cohen"},
{"Name":"Yoni","Age":"20","ID":"Biton"}]

参考这里

于 2012-09-27T11:21:43.113 回答
0

使用该HttpRequest对象,有一个表单变量集合,您可以从该对象获取原始数据。

string rawdata = Request.Form["variable name"];

您必须找到变量名称。

您可以使用调试器查看此内容。

于 2012-09-27T08:43:18.260 回答