Juan M Juan M - 1 year ago 149
Ajax Question

Razor MVC 3 Ajax returns 404

I'm trying to do a basic autocomplete via jquery AJAX, using Razor MVC 3 and Visual Studio 2012 (all of which I had never used before) My code is as follows


$(document).ready(function () {
$("#city").keyup(function () {
var input = $(this).val();

function getCities(input) {
var serviceURL = $("#autocompleteURL").val();
var url =
type: "POST",
url: serviceURL,
data: {
'guess': input
success: function (data) {

Since i learned that it's not possible to use Razor syntax in a JS file, I'm getting the value from the view via a hidden input, like:

<input type="hidden" id="autocompleteURL" value="@Url.Action("AutoCompleteCity", "Controllers/SearchController")" />


public class SearchController: Controller
public ActionResult AutoCompleteCity(string guess)
//database stuff here

I don't know if I'm missing something else. Any help will be greatly appreciated. Thanks!

Answer Source

The convention for the controller name is just the semantic name of the controller, not the file path. Instead of this:

Url.Action("AutoCompleteCity", "Controllers/SearchController")

Try this:

Url.Action("AutoCompleteCity", "Search")

(Note also that you can use the debugging tools in your browser to examine the URL being used in the AJAX call.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download