1

I have this Ajax function:

UpdateFIConfig: function ($appForm) {

    var valid = $appForm.valid();
    //if not valid the validate plugin will take care of the errors
    if (valid) {

        $appForm.serialize();
        $.ajax({
            url: '/IdentifiConfig/DefaultConfiguration/UpdateFIConfig',
            data: $appForm,
            dataType: 'application/json',
            cache: false,
            type: 'POST',
            success: function (data) {
                if (data.Error) {
                    cc.jqUtils.openDialog(data.ErrorDescription, 'Error', 'OK', null, null, null);
                } else {
                    window.location.href = '/IdentifiConfig/DefaultConfiguration';
                }
            }
        });

    }
},

Which serializes data sent from my view into a query string. I know the data is serialized correctly because I have viewed the string with console.log($appForm), and it's correct.

However, my controller never receives the query string. I have removed a lot of code, but this is basically what the controller function looks like:

 [HttpPost]
 public ActionResult UpdateFIConfig(string query)
 {
     NameValueCollection nvc = HttpUtility.ParseQueryString(query);

     System.Diagnostics.Debug.WriteLine(nvc);
 }

I receive a null pointer on the line which tries to parse the query string, and I don't know why. Any help?

2
  • Have you looked at the developer tools in chrome or another browser to see what's actually being sent up? If so, does it match your expecations? Have you checked ModelState.IsValid inside of your controller action to see if there are any model binding errors occurring? Commented Jun 10, 2015 at 15:31
  • No there is no data being sent over the network to the controller, I've already checked the developer console. And yes the model works correctly, I can call other functions and they perform as expected. Commented Jun 10, 2015 at 15:36

3 Answers 3

2

i have the same thing ajax in my project the only different is i don't use dataType but contentType: "application/json; charset=utf-8"

data: "{'query' : '" + $appForm + "'}"
Sign up to request clarification or add additional context in comments.

3 Comments

I changed the contentType to what you suggested and now the function isn't even being called.
also forget since your variable in controller call query data should be like this data: "{'query' : '" + $appForm + "'}",
Man, if I could give you a thousand dollars right now I would. Thank you, that solved my problem!
1

This bit:

$appForm.serialize();

Returns a string that you're never using. serialize won't actually modify the form. You should assign it to a variable and pass that up instead:

var data = $appForm.serialize();

$.ajax({
    url: '/IdentifiConfig/DefaultConfiguration/UpdateFIConfig',
    data: data,
    /* etc */
});

5 Comments

Ok. now I see the correct data being sent up to the network in the developer console, but the function still doesn't receive anything. Could it be something with my dataType?
@TobyCaulk: dataType is the type of data you're expecing back from the server. You might try contentType: text/plain since you don't want the string processed at all by jQuery (at least I don't think you do)
Still nothing in the controller function unfortunately.
@TobyCaulk: When debugging, what happens if you inspect ModelState.IsValid? Does it return true?
@TobyCaulk: I'm actually seeing the same behavior in a test project. Let me fiddle around with it and get back to you
0

There is probably a better way, but I get around this annoyance by accepting an Object with a string property instead of just a string. So do something like:

 [HttpPost]
 public ActionResult UpdateFIConfig(MyTypeWithQry query)
 { ...

and

    $.ajax({ url: '/IdentifiConfig/DefaultConfiguration/UpdateFIConfig',
             data: { 'query' : $appForm },
             dataType: 'application/json',
...

2 Comments

I'll try this, but why does it happen? Do you know?
@TobyCaulk No, I don't know. I suspect something to do with the Content-Type and the content being mismatched or something. You could experiment with text/plain instead of application/json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.