I've been trying to pass JSON data into MVC4 controller, which hasn't worked so far.
Also, I've googled on this a thousand of times.
The key point to solve this issue out there was to stringify the JSON object, define a model and get the parameter as the defined model, or define contentType.
I did those but defining a model for passing JSON into controller.
If I have to define the model every time I try to pass JSON parameter to server, I'm not going to build my app on MVC4, I will give up!
Is there another way to get JSON data from Ajax call in a controller without creating a model for the JSON data?
Here's my example code.
[HttpPost]
public ActionResult DATACRUD(string XmlParms)
{
return Json(new{ data = XmlParms });
}
// Just example.
And Ajax call is.
$.ajax({
type: "POST",
url: "DATACRUD.json",
data: JSON.stringify({data:"test"}),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false, //_async,
success: function (result) {
}
});
Ajax successfully invokes the action in a controller, but the parameter is null.
Please don't tell me defining model for the JSON parameter is the only one way to solve this issue.
It would be very frustrating if I have to do that.
UPDATE:
If I have to match the key name and the param name, I will give up as well.
JSON data usually looks like this in my app.
{
"service": "COMMON",
"method": "MENU_SUBLIST",
"UID": "1000007",
"ULID": "stackoverflow",
"UNM": "queston",
"SITE": "1",
"DEPT": "2",
"LANG": "ko",
"MENUID": "0000",
"STEPMENU": "",
"ACTIONNAME": ""
}
But the thing is that there're many kinds of JSON data including a variety of key names.
I cannot match the names all one by one. In this case, what should I do?
( And just make sure, wrapping the data like this below isn't a solution for me, there's a compatibility issue. )
{ XmlParms : {
"service":"COMMON",
"method":"MENU_SUBLIST",
"UID":"1000007",
"ULID":"stackoverflow",
"UNM":"queston",
"SITE":"1",
"DEPT":"2",
"LANG":"ko",
"MENUID":"0000",
"STEPMENU":"",
"ACTIONNAME":""
}
}
JSON.stringify({XmlParms:"test"})
? – Satpal Jul 22 '14 at 9:00