views:

3652

answers:

4

I am trying to pass some text from a textbox to a controller to get JSON results like so

function invokeAction() {
        var searchText = $("#SearchTextBox").val();

        // Invoke MVC controller action
        $.getJSON("/Home/Results/" + searchText, bindResults);
    }

If I put an alert here I can see that searchText definately has a value, but when I put a break point on this controller action:

 public ActionResult Results(string search)
    {
        var r = from t in db.Restaurants
                where SqlMethods.Like(t.Name, "%" + search + "%") || SqlMethods.Like(t.Postcode, search + "%") || SqlMethods.Like(t.CuisineType.Type, search + "%")
                orderby t.Name ascending
                orderby t.Rating descending
                orderby t.NumOfViews
                    descending
                select t;

        return Json(r.ToList());
    }

The string passed in is null, yet when I check the http context in the debugger my searchtext is a part of the url.

As this is null the query returns no results.

Am I missing something here?

+1  A: 

You will have to fix your route and replace {id} with {search} in order to get it to bind to the correct parameter - try something like this:

routes.MapRoute("search", "Home/Results/{search}", 
  new { controller = "Home", action = "Results" });

If you don't want to do that, you can do it like this by specifying the parametername as a standard querystring paramter

$.getJSON("/Home/Results?search=" + searchText,bindresults);

that will fix the binding.

Christian Dalager
I have changed it t0 $.getJSON("/Home/Results/", { search: searchText }, function(data) { alert("g"); movieView.set_data(data); });where the alert box is there for debugging.The thing is it is never reached.If I put a breakpoint on the controller it does return a JSON result.Any ideas?
dean nolan
Actually it does call that function but only if the controller returns no results. I dont think that set data is getting called when i intend it to. Would I need to clear the old data?
dean nolan
try a basic return Json(new{result="hello"}); and see if that works
Christian Dalager
+3  A: 

I've had some problems returning json from services and I wasn't getting any calls back. it turned out that the json was malformed and I was able to test that and get those errors by handling the error option of the plain ajax call.

$.ajax({
  type: "GET",
  url: "Home/Results/",
  data: { search: searchText },
  dataType: "json",
  error: function(xhr, status, error) {
    // you may need to handle me if the json is invalid
    // this is the ajax object
  },
  success: function(json){
    alert( "Data Returned: " + json);
  }
});
bendewey
Thanks, I used that and have trapped an error. I also used firebug which told me I have a circular reference. I think I need to sort my database out a bit.
dean nolan
Cool, I'm glad, so do I get the answer?
bendewey
Yes bendewey, you can get the answer ;)
dean nolan
A: 

I have tried this approach, it works very well, thanks a lot.

Gracepig
A: 

Here is an elaborated tutorial on how to work with jQuery.getJSON(). Hope it'll help you. JSONP First Timer

adnan