Grayson Mitchell
Grayson Mitchell

Reputation: 1187

asp mvc: how to pass parameter to controller using jquery api?

I am following the following tutorial (http://www.highoncoding.com/Articles/642_Creating_a_Stock_Widget_in_ASP_NET_MVC_Application.aspx) on using ajax to render a partial form , but in this example parameters are not passed, and I have not been able to work out how to do it...

This code works with no parameter

function GetDetails() {
$("#divDetails").load('Details'); 
}

This is my attempt to add a parameter, but does not work (cant find action)

function GetDetails() {
$("#divDetails").load('Details?Id=20'); 
}

Upvotes: 0

Views: 2644

Answers (2)

ten5peed
ten5peed

Reputation: 15890

The jQuery.load() method can take an object and will turn the request into a POST and ASP.NET MVC should do the rest.

So it should work if you try this:

function GetDetails() {
    $("#divDetails").load('Details', {Id: 20}); 
}

HTHs,
Charles

Ps. The default route should beable to handle Controller/Action/Id, so you should beable to do something like $("#divDetails").load('Controller/Details/20');

Upvotes: 0

Dave Swersky
Dave Swersky

Reputation: 34810

Paramters in MVC are added like this:

http://mysite.com/action/parameter

Change your question mark to a forward slash, and make sure your path is referenced correctly from your jquery code. You can use Firebug in Firefox or Fiddler in IE to look at the GET operation to make sure the URL for the request is properly formed.

Upvotes: 1

Related Questions