DotNetUser
DotNetUser

Reputation: 239

pass multiple parameters using $.ajax to an MVC method

I want to use:

$.ajax({url:'controller/method ?startDate=' +  startDate + '& endDate=' + endDate});

In the controller I have a method like this:

public PartialView GetChartDate(DateTime? startDate, DateTime? endDate){}

When I do this I end up passing null for endDate to the MVC method even though it has value. How do I pass multiple params to the MVC method?

Ideas and suggestions greatly appreciated !

Upvotes: 2

Views: 17900

Answers (3)

Ahsan Attari
Ahsan Attari

Reputation: 1079

 $.ajax({
            type: "post",
            url: "/xyz/xyz",
            data: "param1=" + value+ "&param2=" + value,

        });

i think it works, because i am using it and working with this

Upvotes: 0

alphadogg
alphadogg

Reputation: 12900

If you actually cut-and-pasted your code, there's a space between the ampersand and the variable.

$.ajax({url:'controller/method ?startDate=' + startDate + '& endDate=' + endDate});
                                                            ^

Not sure if that is what does it, but check it out.

Upvotes: 8

Bartosz
Bartosz

Reputation: 3358

Don't create query string on your own. Rely on jQuery:

$.ajax({
     url: '/controller/method',
     data: { startDate: startDate, endDate: endDate}
     // ...
});

Upvotes: 12

Related Questions