Click here to Skip to main content
15,886,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
For OnChange event of a dropdown, I am making a ajax call. In the url path, when I give the Controller/Action path it does not hit the method at all.
My code looks like:

JavaScript
function FillHO() {
    //alert('Test');
    var stateId = $('#ddlHO').val();
    $.ajax({
        type: "POST",
        url: '@Url.Action("SetSelectedHO","../../Controllers/ReportsController")' 
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: '{SelectedValue: "' + stateId + '" }',
        success: function (response) {
            alert('Test');
        },
        failure: function (response) {
        alert(response.d);
    }
    });
}


What I have tried:

I have tried '../../Controllers/ReportsController/SetSelectedHO'
'ReportsController/SetSelectedHO'
Posted
Updated 21-Jul-19 23:58pm
Comments
santosh.yadav198613 21-Oct-16 11:54am    
Try calling action from Rest Client and check what you are missing
ZurdoDev 21-Oct-16 12:00pm    
Implement the error function and see what is happening.

You don't need to specify the path when using Url.Action() method. You need to specify the Controller name instead.

So if you have a Controller that was named ReportsController, then you could try doing something like this:

JavaScript
url: '@(Url.Action("SetSelectedHO","Reports"))'


Note that the suffix Controller was omitted when referencing a Controller name. In other words, your ReportsController can be referenced as Reports only. That's part of the convention.

For more information about the other overload methods of Url.Action() method see:UrlHelper.Action Method (System.Web.Mvc)[^]
 
Share this answer
 
Comments
Gopal Rajbanshi 22-Jul-19 5:59am    
useless
CHill60 22-Jul-19 7:22am    
Useless comment - if you thinkthere is a problem with the solution and are going to go to the trouble of posting a comment at least explain why you think there is a problem
Solved.
It worked without giving an controller reference in url

JavaScript
url: "SetSelectedHO",


Thanks.
 
Share this answer
 
Use dev tools (F12 in Chrome) to inspect the JavaScript that the client has. You might find the URL supplied for the call is not what you expect it to be.

Does this...

C#
url: '@Url.Action("SetSelectedHO","../../Controllers/ReportsController")'


... generate a valid URL? It looks as though you're setting a literal and not the return value of the @URL.Action method.
 
Share this answer
 
v2
This worked for me:

url:'../Controller/action'
 
Share this answer
 
Comments
Member 14641047 31-Oct-19 21:20pm    
Thanks it work for me on laravel, on ubuntu
Thanks for solution 4 its work forme , on laravel on ubuntu
 
Share this answer
 
Comments
CHill60 1-Nov-19 10:40am    
If you want to comment on a solution then use the "Have a Question or Comment?" link next to it. Do not post comments or questions as solutions to other members' posts!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900