-1

I just want to know why it's necessary for .NET to match parameter name with the JSON object's key name?

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": "" 
}

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) {
        }
});

And my c# action code here..

[HttpPost]
public ActionResult DATACRUD(string jsondata)
{
    return Json(new{ fromMVC = jsondata});
}
// Just example.

jsondata is here null because I didn't match the key name.

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":""
        }
    }

Here question No.1 Why do I have to match the key name with the param name?

It just does? there's gotta be a reason, and I want to know why.


And what I want to do is...

{         
        "service":"COMMON",
        "method":"MENU_SUBLIST",
        "UID":"1000007",
        "ULID":"stackoverflow",
        "UNM":"queston",
        "SITE":"1",
        "DEPT":"2",
        "LANG":"ko",
        "MENUID":"0000",
        "STEPMENU":"",
        "ACTIONNAME":""
}

to pass this JSON data into the action, DATACRUD I specified above

I want DATACRUD action to take the JSON data and consume it whatever the key name is.

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.

So receiving the JSON data after making a model is the last thing I need.

In this case, how am I supposed to do?

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....

What I have to do?

hina10531
  • 3,938
  • 4
  • 40
  • 59
  • 1
    The default model binder is what is causing this behavior. It makes a lot of assumptions about naming schemes to get from HTTP request -> C# code. You could write your own custom model binder, or directly read from the stream like in py3r3str's answer. – Dan Schnau Jul 22 '14 at 12:48

2 Answers2

1

The MVC routing engine dictates that the parameter names must match, as that is how it knows what to populate since everything comes through as strings to the server. The MVC plumbing will be searching through the query portion of the URL, and even searching fields in a form on a POST to populate all of your parameters.

Having a hundred models is not that bad for a complex project. However, it can be a pain if you have to go back and retrofit your entire application.

No matter what you do, you'll need to make sure that your JavaScript variable names match those of your Action method parameters, which shouldn't be a problem since you're writing both sides.

krillgar
  • 12,596
  • 6
  • 50
  • 86
0

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 });
}
Community
  • 1
  • 1
py3r3str
  • 1,879
  • 18
  • 23