0

I work on an ASP.NET MVC project. I have to pass two parameters to an action in my controller. the first is a serializable object, and the second one is an integer. First time I tried to pass only one parameter, the serializable object. There is no problem, but when I add the second parameter, the serializable object doesn't delivered (null value), but the integer parameter delivered successfully.

this is my action look like :

    [HttpPost]
    public bool MyAction(MySerializableObject myData, int intParameter)
    {..}

and this is how I try to pass the parameters :

 $('#submit-button').click(function () {        
    var formData = $("#MyForm").serialize();
    var posturl = '/MyController/MyAction';
    var retUrl = '/MyCOntroller/SomeWhere';
    ...
    $.post(posturl, { myData: formData, intParameter: '5005' }, function (result) {
        if (result == 'True') {
            location.href = retUrl;
        }
        else {
            alert('failed');
        }
    });
});

Anyone can explain about it ? how can it happens and how to solve the problem ?

thanks.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ichiro Satoshi
  • 301
  • 1
  • 8
  • 17

2 Answers2

0

this may be a bit of a longshot but have you tried swapping the order of the parameters around (IE public bool MyAction(int intParameter, MySerializableObject myData) The reason im asking is that it may be that your client side serialize isnt working quite right.

If not your best bet is to take a look at whats actally getting posted to the server. Open up firebugs net tab or similar in webkit and take a look at whats actually going back to the server.

undefined
  • 33,537
  • 22
  • 129
  • 198
0

You could use the following plugin (serializeObject) instead of .serialize:

var formData = $('#MyForm').serializeObject();
// add some data to the request that was not present in the form
formData['intParameter'] = 5005;

var posturl = '/MyController/MyAction';
var retUrl = '/MyCOntroller/SomeWhere';
...
$.post(posturl, formData, function (result) {
    if (result == 'True') {
        location.href = retUrl;
    }
    else {
        alert('failed');
    }
});
Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928