views:

177

answers:

2

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'); 
}
+1  A: 

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.

Dave Swersky
Not just replace should be 'Details/20'
Dustin Laine
thanks, this does work; As long as the parameter is called Id
Grayson Mitchell
A: 

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');

Charlino
Thats the same thing in his post, see my comment on @Dave Swersky's answer.
Dustin Laine
That won't work if he's trying to use the View action parameter.
Dave Swersky
@durilai - no it's not, adding the object turns the .load into a post. @Dave Swersky - yes it should work, when doing databinding to action parameters ASP.NET MVC will check the POST values.
Charlino
Sorry, POSTing like that still won't work without an ActionFilter to parse the JSON format. The code in your PS should work.
Dave Swersky
@Dave Swersky - I'm no expert on jQuery but I'm pretty sure passing in `{Id: 20}` doesn't mean that you'll get JSON in the POST values... it means that you'll have a key value pair in your post values. So therefore, ASP.NET MVC will handle it and you'll get it in your action parameter.
Charlino
@Charlino: you're probably right about that, the curly braces confused me there for a minute! I've never tried that syntax... learn something new every day!
Dave Swersky
thanks, works great.
Grayson Mitchell