views:

487

answers:

1

Ok. So I have two elements on a page which are not located in a single form. I need these to be posted back to my controller via the JQuery.Ajax() method. Now the problem I have is that while my parameters do correctly post they only do so if I set them as strings and they include the parameter name in the value. So:

Public ActionResult Method(String Age1, String Age2)
{
    Age1=23; Age2=43
}

I get

Public ActionResult Method(String Age1, String Age2)
{
  Age1="Age1=23"; Age2="Age2=43"
}

Which is irritating. Is there any way to make sure MVC will map the parameters correctly and only take in the correct values? I really want it to do:

Public ActionResult Method(Int32 Age1, Int32 Age2)
{
  Age1="Age1=23"; Age2="Age2=43"
}

The jQuery call:

$.ajax{(

//other stuff
data: { Age1: $('.id').val(), Age2: $('.id2').val() };
)};

Opps, It was because in my actual code I was using Serialize! DUH!

+2  A: 

I honestly don't see anything wrong with what you're doing, provided you're actually passing the numbers 23 and 43 via $.ajax, and not "Age1=23" and "Age2=43". If you were, int certainly wouldn't work as the framework can't cast those strings to integers.

It might be worth it to change your ajax() call to this, just to see what happens.

data: { 
    Age1: parseInt($('.id').val()),
    Age2: parseInt($('.id2').val())
}
Stuart Branham
Well that's the problem, the strings are being passed.
Damien