views:

96

answers:

3

Hello,

I have got this working with a local data source but not remotely. It uses the Jquery library and I have followed the instructions on the Jquery UI site. This is the code I have (which does not work). Can anyone a) amend this code to work b) show code of a working example?? Thanks:

JQUERY

  $('#countries').autocomplete({
         source: "/Trip/Lookup",
         minLength: 0,
         focus: function (event, ui) {
             $('#countries').val(ui.item.label);
             return false;
         },
         select: function (event, ui) {
             return false;
         }
  }).data("autocomplete")._renderItem = function (ul, item) {
      return $("<li></li>")
          .data("item.autocomplete", item)
          .append("<a>" + item.label + "</a>")
          .appendTo(ul);
  };

ACTIONRESULT

    public ActionResult Lookup(string q, int limit)
    {
        List<DestinationVM> list = new List<DestinationVM>();
        list.Add(new DestinationVM { Destination = "England", Cost = 12 });
        list.Add(new DestinationVM { Destination = "New Zealand", Cost = 10 });
        list.Add(new DestinationVM { Destination = "Australia", Cost = 8 });

        var data = from s in list select new { s.Destination, s.Cost };

        return Json(data);
    }
A: 

You should use the Firebug plugin for Firefox. You'll be able to see any errors in the Net > XHR tab.

alt text

jessegavin
That is the error I get:http://localhost:1489/Trip/Lookup?term=a 500 Internal Server Error 2.07sThe parameters dictionary contains a null entry for parameter 'limit' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Lookup(System.String, Int32)' in 'NewBudgetSite.Controllers.TripController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.<br>Parameter name: parameters
A: 

Change your action definition to look like this:

public ActionResult Lookup(string term)

The autocomplete plugin sends a request with a query parameter called term which contains the characters that the user has typed into the text box so far.

Also, try changing your Linq projection to this:

var data = from s in list select new { label = s.Destination, value = s.Cost };

The autocomplete plugin expects either a flat array of values or an array of JSON objects which have a label and value property.

joshperry
this did not resolve my problem. Any other ideas?
@user Updated the answer for another fix that you'll need to make.
joshperry
A: 

The action on the controller was wrong. It should have been:

The changes are in the input parameter and the inclusion of the return parameter JsonRequestBehavior.AllowGet. It now works.

    public ActionResult Lookup(string term)
    {

        var result = _TripRep.GetAutoCompleteDestination(term, 5);

        var data = from s in result select new { label = s.Destination, value = s.Cost };

        return Json(data, JsonRequestBehavior.AllowGet);

    }
I see that besides the request behavior change that my answer solved your issues. StackOverflow etiquette would usually mean you'd accept my answer and maybe add a comment to the effect of the additional requirement. Just for future reference as people are unlikely to spend much time answering questions for users with a low accept rate.
joshperry