Multiple parameters with POST and MVC

Ok. Thus, I have two elements on the page that are not in the same form. I need them to be sent back to my controller using the JQuery.Ajax () method. Now the problem is that although my parameters are positioned correctly, 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"
}

      

This is annoying. Is there a way to make sure MVC renders the parameters correctly and only accepts correct values? I really want to do this:

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

      

JQuery call:

$.ajax{(

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

      

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

+1


source to share


1 answer


I honestly don't see anything wrong with what you are doing if you are actually passing 23 and 43 through $ .ajax and not "Age1 = 23" and "Age2 = 43". If you were, int would certainly not work, as the framework would not be able to translate those strings to integers.

It might be worth changing your ajax () call to see what happens.



data: { 
    Age1: parseInt($('.id').val()),
    Age2: parseInt($('.id2').val())
}

      

+2


source







All Articles