为什么我有相匹配的参数名称从MVC4 Web应用程序Ajax调用获取JSON数据? [英] why do I have to match the parameter name to get json data from ajax call in MVC4 web app?
问题描述
我只是想知道为什么它是必要的.NET匹配参数名的JSON对象的键名?
I just want to know why it's necessary for .NET to match parameter name with the JSON object's key name?
快速code $ P $这里PVIEW ...
Quick code preview here...
var json = {
"service": "COMMON",
"method": "MENU_SUBLIST",
"UID": "1000007",
"ULID": "stackoverflow",
"UNM": "queston",
"SITE": "1",
"DEPT": "2",
"LANG": "ko",
"MENUID": "0000",
"STEPMENU": "",
"ACTIONNAME": ""
}
好了,让我们通过Ajax调用控制器中的一个动作。
Okay, Let's call an action in a controller through Ajax.
$.ajax({
type: "POST",
url: "DATACRUD.json",
data: JSON.stringify(json),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false, //_async,
success: function (result) {
}
});
和我的C#动作code在这里..
And my c# action code here..
[HttpPost]
public ActionResult DATACRUD(string jsondata)
{
return Json(new{ fromMVC = jsondata});
}
// Just example.
jsondata在这里空,因为我不匹配的键名。
jsondata is here null because I didn't match the key name.
有关DATACRUD获取JSON数据,我必须做的是这样的。
For DATACRUD to get the JSON data, I have to do like this.
{ jsondata : {
"service":"COMMON",
"method":"MENU_SUBLIST",
"UID":"1000007",
"ULID":"stackoverflow",
"UNM":"queston",
"SITE":"1",
"DEPT":"2",
"LANG":"ko",
"MENUID":"0000",
"STEPMENU":"",
"ACTIONNAME":""
}
}
在这里提问1号 为什么我的密钥名称与参数名称相匹配?
这少了点?还有得是有原因的,我想知道为什么。
It just does? there's gotta be a reason, and I want to know why.
而我想要做的是...
{
"service":"COMMON",
"method":"MENU_SUBLIST",
"UID":"1000007",
"ULID":"stackoverflow",
"UNM":"queston",
"SITE":"1",
"DEPT":"2",
"LANG":"ko",
"MENUID":"0000",
"STEPMENU":"",
"ACTIONNAME":""
}
此JSON数据传递到行动,DATACRUD我上面指定
我想DATACRUD采取的措施JSON数据,并使用它的任何键名。
I want DATACRUD action to take the JSON data and consume it whatever the key name is.
还有这另外一个答案。答案是创建一个模型,JSON数据,并接受它作为一种模式类型,并获得模型作为字符串。
There's another answer for this. The answer is to create a model for JSON data and receive it as a model type, and get the model as string.
但是,在定义模型可以是不可能在我的应用程序。这可能会导致一百模型创建的。
But defining models cannot be possible in my apps. It could cause a hundred of model creation.
所以接收制作模型后JSON数据是我需要的最后一件事。
So receiving the JSON data after making a model is the last thing I need.
在这种情况下,我怎么办?
没有按键名称匹配是允许的。
No key name matching is allowed.
没有生成模式是允许的。
No generating model is allowed.
没有第三方框架是允许的。
No third party framework is allowed.
我认为可能的答案缩小到几......
I think the possible answers narrow down to a few....
我有什么关系?
推荐答案
在帖子<一个基地href="http://stackoverflow.com/questions/13041808/mvc-controller-get-json-object-from-http-body/13116824#13116824">MVC控制器:从HTTP身体得到JSON对象? 你的动作应该是:
Base on post MVC controller : get JSON object from HTTP body? You action should be:
[HttpPost]
public ActionResult DATACRUD()
{
Stream req = Request.InputStream;
req.Seek(0, System.IO.SeekOrigin.Begin);
string json = new StreamReader(req).ReadToEnd();
return Json(new { fromMVC = json });
}
这篇关于为什么我有相匹配的参数名称从MVC4 Web应用程序Ajax调用获取JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!