4

I am having a (I think stupid) problem.

I have a controller, Index method, and its view has some JQuery functions.

It works fine, and the JQuery methods work perfectly.

the link I use is

http://localhost:54209/TestInput/

but if I put

http://localhost:54209/TestInput/Index

the JQuery functions do not work. From what I know they should act exactly the same.

That is the only thing that I change

I really appreciate your help. This has been driving me crazy during the last couple of hours!

For example, This is my script

<script>
  $(document).ready(function() {
    $('select#testCategoriesUniqueId').change(function() {
        var testCategory = $(this).val();
        $.ajaxSetup({ cache: false });
         alert ("AAA");
        $.ajax({
            url: "TestInput/listTestCases/" + testCategory,
            dataType: "json",
            type: 'post',
            success: function(data) {
                $("#testCasesUniqueId").removeOption(/./);
                for (var i = 0; i < data.length; i++) {
                    var val = data[i].Value;
                    var text = data[i].Text;
                    $("#testCasesUniqueId").addOption(val, text, false);
                }
            }
        });
    });
});

In both cases, I get an alert, but In the second link, I can not call the controller.

It doesn't call the listTestCases method of my controller.

Update:

So I tried to use parameters instead of the exact link, I still have the problem, I got both sources, and got a diff, the only difference is

<form name="aspnetForm" method="post" action="Index" id="aspnetForm">

vs.

<form name="aspnetForm" method="post" action="TestInput" id="aspnetForm">

and

<form action="/TestInput/Index" method="post">

vs.

<form action="/TestInput" method="post">

Which I beleive has nothing to do with the jQuery.

I still see the laert in both cases. but the JQuery works in ~/TestInput and not with the ~/TestInput/Index.

Athena
  • 83
  • 1
  • 2
  • 9
  • Can you post your markup where you include the references to jquery scripts? I'm speculating that maybe they are relative and aren't being found. Also, try using debug console of your browser(F12 in Chrome, switch to Console tab, reload) to see if there are any errors. – AaronLS Nov 17 '12 at 02:03
  • The js files are probably being included correctly, or he wouldn't be getting the alert. – Zack Nov 17 '12 at 20:20

1 Answers1

5

this is why you shouldn't hardcode urls in an asp.net mvc app. your problem is that your ajax url is a relative url. when you load the page using url http://example.com/TestInput/ the ajax url ends up being something like http://example.com/TestInput/TestInput/listTestCases or maybe http://example.com/TestInput/listTestCases

When you use the url http://example.com/TestInput/Index your ajax url ends up being http://example.com/TestInput/Index/TestInput/listTestCases

Insetead you should use one of the Html helpers to declare your ajax url like this. (using razor syntax)

<script>
  $(document).ready(function() {
    $('select#testCategoriesUniqueId').change(function() {
        var testCategory = $(this).val();
        $.ajaxSetup({ cache: false });
         alert ("AAA");
        $.ajax({
            url: "@Url.Action("listTestCases")"+ "/" + testCategory,
            dataType: "json",
            type: 'post',
            success: function(data) {
                $("#testCasesUniqueId").removeOption(/./);
                for (var i = 0; i < data.length; i++) {
                    var val = data[i].Value;
                    var text = data[i].Text;
                    $("#testCasesUniqueId").addOption(val, text, false);
                }
            }
        });
    });
});
</script>

Update for ASPX View Engine:

If you are using the ASPX view engine insted of Razor use this syntax.

<script>
  $(document).ready(function() {
    $('select#testCategoriesUniqueId').change(function() {
        var testCategory = $(this).val();
        $.ajaxSetup({ cache: false });
         alert ("AAA");
        $.ajax({
            url: "<%=Url.Action("listTestCases")%>"+ "/" + testCategory,
            dataType: "json",
            type: 'post',
            success: function(data) {
                $("#testCasesUniqueId").removeOption(/./);
                for (var i = 0; i < data.length; i++) {
                    var val = data[i].Value;
                    var text = data[i].Text;
                    $("#testCasesUniqueId").addOption(val, text, false);
                }
            }
        });
    });
});
</script>

Be sure to use Firebug or F12 dev tools to double check the exact url you need.

Zack
  • 2,291
  • 2
  • 23
  • 38
  • Also see this for different between Url.Content and Url.Action. Url.Content for things that don't have an action, like images and references to script files: http://stackoverflow.com/questions/8186163/mvc-url-content-vs-url-action – AaronLS Nov 17 '12 at 02:04
  • Thanks Zack, I tried url: "@Url.Action("listTestCases")"+ "/" + testCategory, but I didn't even get the Alert – Athena Nov 19 '12 at 18:04
  • then I tried and url: "@Url.Action(\"listTestCases\")"+ "/" + testCategory, I got the alert this time, but my action was not called. I'm using aspx files. should I use something other that @Url.Action... ? – Athena Nov 19 '12 at 18:05
  • I also tried : url: "@Url.Action(\"listTestCases"+ "/" + testCategory + "\")", – Athena Nov 19 '12 at 18:11
  • i updated the answer for the ASPX view engine. if you are still having trouble after this, you can always view source and see if the url being written onto the page for the ajax call is correct and make any adjustments. – Zack Nov 19 '12 at 18:58
  • Thanks Zack. Still doesn't work. I updated my question (the source files are almost the same) – Athena Nov 20 '12 at 02:17
  • this should work, unless you are having some other issue. btw, the form tag should't be related to what you are asking about. What version of Visual Studio and MVC are you using? If I have time I'll throw together an example. – Zack Nov 21 '12 at 21:21