ASP.NET MVC: Posting JSON to Controller
- by JamesBrownIsDead
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.