ASP.NET MVC: Posting JSON to Controller

Posted by JamesBrownIsDead on Stack Overflow See other posts from Stack Overflow or by JamesBrownIsDead
Published on 2010-05-18T21:28:08Z Indexed on 2010/05/18 21:30 UTC
Read the original article Hit count: 297

Filed under:
|
|
|

I've got this in my controller:

    [HttpPost]
    public void UpdateLanguagePreference(string languageTag)
    {
        if (string.IsNullOrEmpty(languageTag))
        {
            throw new ArgumentNullException("languageTag");
        }

        ...
    }

And have this jQuery code POSTing to the controller:

           jQuery.ajax({
                type: 'POST',
                url: '/Config/UpdateLanguagePreference',
                contentType: 'application/json; charset=utf-8',
                data: '{ "languageTag": "' + selectedLanguage + '" }'
            });

When I try to the code, however, I get the error:

Server Error in '/' Application.
Value cannot be null.
Parameter name: languageTag

What's the problem? Isn't this how to POST JSON to an MVC Controller? I can examine the POST using Fiddler and see that the request is correct. For some reason, UpdateLanguagePreference() is getting a null or empty string.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about mvc