views:

103

answers:

5

I have an ASP.Net MVC application. I am trying to post data to an action, and then just render the output. I have a page where you can view a product, so www.mysite.com/product/details/1 shows the product. On this page, I render a partial view through RenderAction() that contains some pricing logic. I want the user to select some options and then have jquery post the options to get the new price.

However, when I call

$.post({
  url : "price/getprice",
  data : {"options" : chosenOptions},
  success : function(data) {
              $("#price").text(data);
            }
});

The url is posting to www.mysite.com/product/details/price/getprice which doesn't exist. I've tried posting to "~/price/getprice/" but it does the same thing. Why won't it go to my PriceController's GetPrice(FormCollection form) action??

This should post to www.mysite.com/price/getprice. When I look in the Net tab in firebug, it says it's posting this:

http://localhost:42427/Product/Details/%5Bobject%20Object%5D

If I look at the response in Firebug, the application is throwing an exception:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'PrintPlaceWebsite.Controllers.ProductController'.

But I'm not trying to post to that location so...ugh.

+2  A: 

Well you specified a relative URL, so if this code is on site www.mysite.com/a/b/c, the URL will point to /a/b/price/getprice.

You probably want to specify an absolute URL:

$.post({
  url : "/price/getprice",
  //     ^-- leading slash
  data : {"options" : chosenOptions},
  success : function(data) {
              $("#price").text(data);
            }
});
Felix Kling
+1 - Your example needs a tweak though, it would be pointing to `a/b/price/getprice`, since the `c` has no trailing slash :)
Nick Craver
@Nick: Thank you :)
Felix Kling
I've tried that. Maybe I have a routing issue or something?
scottm
@scottm: If you do that, what says Firebug? If you have a routing issue, than it is a server problem.
Felix Kling
@scottm - It shouldn't be related to routing if you're seeing the URL in your question in Firebug/Chrome console. It may be a caching issue with your script files? There's no way that `url` in the `$.post()` call should go to the URL you're seeing.
Nick Craver
@Felix, the last little bit is the result in firebug when using this path (/price/getprice)
scottm
@Nick, that's why I'm pulling my hair out :)
scottm
A: 

As far as JQuery knows it is basing its AJAX calls on the current location which, im guessing, is www.mysite.com/product/details/. Just as if you had a link to "myPage.html" would try to go to www.mysite.com/product/details/myPage.html.

I would suggest you resolve the URL in your View with something like..

<% = ResolveUrl("~/price/getprice") %>
Stuart
+1  A: 

How do you get your current URL?

Are you using Url.Action()?

Peter Forss
The link is in the javascript file.
scottm
Then you specify the URL (from Url.Action()) in a variable on the page before you include your JS file, then you use that var in your code.
Peter Forss
A: 

It seems that the URL router is mixing things up. Just replace the path with an absolute (full) one.

$.post({
  url : "http://mywesbite.com/price/getprice",
  data : {"options" : chosenOptions},
  success : function(data) {
          $("#price").text(data);
        }
});
Makram Saleh
I've tried that as well, and the same thing happens.
scottm
A: 

So, it turns out it was a little typo on my part (an everyone else's because you all copied it).

To reiterate:

$.post({
  url : "/price/getprice",
  data : {"options" : chosenOptions},  <<<<< Right here. dang.
  success : function(data) {
          $("#price").text(data);
        }
});

Do you see it?

$.post({
  url : "/price/getprice",
  data : { options : chosenOptions},  <<<<< no quotes. arg.
  success : function(data) {
          $("#price").text(data);
        }
});
scottm
This might be the solution but it is not *bulletproof*. You can have issues with it just when switching between IIS and Visual Studio webserver or when using virtual directory in IIS and when not using one. You need to use Url.Action() like this $("#Url").autocomplete('<%=Url.Action("getprice", "price", new { options = "..."}) %>', {...});
mare