2

I have a jquery code which opens a dialog. This dialog is a partial view which renders from the response of a jquery Ajax call "http://test.com/ControllerA/ViewDetails". Ajax call code looks like below

$.ajax({
    url: "ViewDetails",
    type: "GET",
    dataType: "html",

The dialog box has button which has to make another Jquery Ajax call (this has go against a different controller and action). Ajax code looks like below.

$.ajax({
    url: "ControllerB/Search",
    type: "GET",
    dataType: "html",

The above ajax call fails to find search action because The URL will get changed to http://test.com/ContollerA/ControllerB/Search.

I feel this is something related to a route config. But i need some directions from you all.

2
  • 1
    add "~/" before the url i.e "~/ControllerB/Search" , it represent the root/url further path , so in your case test.com/ControllerB/Search Commented Jan 30, 2014 at 19:59
  • If you are externalizing the urls to separate javascript variable assign the urls in hidden fields and then use the value of hidden field in javascript file. Commented Jan 30, 2014 at 20:25

2 Answers 2

6
$.ajax({
    url: "@Url.Action("ViewDetails", "ControllerA")", 
    type: "GET", 
    dataType: "html",

and

$.ajax({
    url: "@Url.Action("Search", "ControllerB")", 
    type: "GET", 
    dataType: "html",

This way you're using the route table and not generating urls willy nilly

Sign up to request clarification or add additional context in comments.

Comments

1

The best way is to use

 url: '../ControllerA/ViewDetails'

it worked for me when the culture is in url

1 Comment

this works in a partial view rendered in a same page. In my case i was opening a dialog box. So i need to use the @Url.Action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.