There is a need to get some data for an autocomplete ajax request. I reviewed a bunch of examples in the internet and in each of them there is something like this:

data-autocomplete-source='@Url.Action("MyAction", "MyController")' 

However, this does not work. Perhaps you need to make the correct routing, but it does not work for me. If you write a direct link instead of target.attr("data-autocomplete-source") , as shown in the code, then everything works.

 $(document).ready(function () { $(".searchField").each(function () { var target = $(this); target.autocomplete({ source: function (request, response) { $.post("/MyController/MyAction/", request, response); }, minLength: 1 }); }); }); 

Routing:

 routes.MapRoute ( "AutocompleteSearch", // Route name "{controller}/{action}", // URL with parameters new { controller = "MyController", action = "MyAction" } 

    1 answer 1

    In routing, it is customary to specify the names contrller and action without endings ...Controller and ...Action .

    Accordingly, the same principle is observed in the method of obtaining Url:

     @Url.Action("My", "My") 

    I still recommend giving clearer names so that there is no confusion.

    • I wrote MyController and MyAction as an example. Of course I point without endings. That's not the problem - user3576767
    • @ user3576767 what do you get in place @Url.Action() after rendering the page? - Romario
    • After rendering the same expression @ Url.Action ("MyAction", "MyController") - user3576767
    • @ user3576767 Apparently, the Razor engine perceives this as text, not as a calculated expression. Look closely at what your View looks like and where the specified line is located. You can add your View to the question - let's see together. - Romario
    • and do other Razor teams work? I had experience in 2015 studios when View files just stopped working. Team devenv.exe / ResetUserData - nikita helped