MVC3 - Debugging the controller action called by an Ajax call?

You should hit the break point if its making it that far. My guess is that you need to stringify your object to get the json .net is going to understand. Or, since its such a simple object, just do something like.

Up vote 1 down vote favorite share g+ share fb share tw.

I am attempting to populate a dropdown based on the value selected in another dropdown. I am using this guy's example, but have not gotten it to run successfully yet. I have a successful Ajax call that is fired on the change event of the parent dropdown.

I've verified that the data passed to the Ajax function is correct. However, the result is an error (200; unexpected character in JSON result). I've been trying to figure out how to debug the a) controller action that is called in the Ajax call and b) the function that feeds a list of results to the controller action.

Can anybody help me figure out how to debug a) and b)? Visual Studio 2010 doesn't offer a lot of help for debugging the targets of Ajax calls, it seems. Here's the code I have: 1) The controller action (BreedController) that calls a list helper class function to supposedly return a JSON object back to the Ajax successful callback.

// // Retrieve JSON object containing breeds for a given species // HttpPost public JsonResult BreedsBySpecies(int? Id) { ListHelper lh = new ListHelper(); return Json(new { items = lh. GetBreedsBySpecies(id) }, JsonRequestBehavior.

AllowGet); } 2) The function that should return a SelectItemList of breeds given a species ID. This is called by the controller action. Public List GetBreedsBySpecies(int?

SpeciesID) { var breed = from be in db. Breeds select b; if (speciesID. HasValue) { breed = breed.

Where(b => b. SpeciesID == speciesID); } List lst = new List(); foreach (var item in breed) { lst. Add(new SelectListItem { Text = item.

Description, Value = item.BreedID.ToString() }); } return lst; } 3) The javascript function that does the Ajax call. I've confirmed that this is getting the right values (e.g. , "/Breed/BreedsBySpecies" to get to the right controller action and formData contains the right species ID) function selectFromAjax(url, formData, target) { $(target). Html(""); if (formData.

Id) { $. Ajax({ type: 'POST', url: url, data: formData, dataType: 'text json', contentType: 'application/json; charset=utf-8', success: function (data, textStatus) { if (data) { $(data. Items).

Each(function () { $(target). Append($(""). Attr("value", this.

Value). Text(this. Text)); }); $(target).change(); } }, error: function (xhr, ajaxOptions, thrownError) { alert(xhr.

Status); alert(thrownError); } }); } else { $(target).change(); } } ajax asp. Net-mvc-3 visual-studio-debugging link|improve this question asked Dec 2 '11 at 19:54Neal Helman155.

– Neal Helman Dec 2 '11 at 20:13 1 open the website and firebug. Refresh the website and in firebug click console to see the errors. It should show you the error.

If not, go to scripts tab and find your file and code there. You can create breakpoints and debug your javascript there too. – bobek Dec 2 '11 at 20:14 That shows me that the Ajax error callback is being triggered, but it doesn't get me into the C# code at all.

As far as I can tell, the Ajax call is working as expected. It's the C# code I'm trying to debug, but breakpoints on it aren't being hit. – Neal Helman Dec 2 '11 at 20:21 1 try removing the success and failure from your ajax call, then It should come up with an error, and when you click on it and select html, it will show you the error page, just like regular asp.net error page.

– bobek Dec 2 '11 at 20:22.

You should hit the break point if its making it that far. My guess is that you need to stringify your object to get the json .net is going to understand. Or, since its such a simple object, just do something like: data: "{'id': '" + formData.

Id + "'}" Edit since you are just passing in an id, you could technically just use the url to get where you want, passing no data at all. You'd just do something like this: url: url + '/'+ formData. Id this does tightly couple your routes and javascript, which isn't ideal, but it gets the job done.

And it removes the need to pass any data to the data parameter.

I've tried breakpoints in both the controller action and the list helper function, but it never hits either. I assumed (uh oh, there's that word again), that it was because the controller action is being called in the context of an Ajax call. Should a breakpoint in the controller action or helper function be hit regardless?

– Neal Helman Dec 2 '11 at 20:06 @NealHelman, yes. The issue is that the method is not being called due to the mvc modelbinder not being able to parse your json into something that you have a method signature for. I've actually thought of a simpler alternative that will work for these simple one parameter id things.

See my answer. – nathan gonzalez Dec 2 '11 at 20:12 Thanks for that. That gets me on to the next step, although I still can't tell what is going on in the controller action and helper function, which are really what I need to be looking at.

– Neal Helman Dec 2 '11 at 20:27 I got to debug the VS2010 code. I'm such a dumb@55. I had an AuthorizeAttribute on the controller that was being called.

Simplifying the ajax call seemed to be what I needed to be able to tell what was going on. – Neal Helman Dec 2 '11 at 20:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions