I am new to MVC, and am trying to make a simple site work. I'm starting to wonder if it is really worth it...I could have had this site up and running with "old-school" ASP.Net two or three times already...but that is beside the point ;-)
How can I get my controller to return a JSONResult without the browser prompting me to save the response as a file? Here is the JavaScript that calls the action:
$("select#PageId").change(function() {
var id = $("#PageId > option:selected").attr("value");
$.getJSON('FindCategories/', { PageId: id },
function(data) {
if (data.length > 0) {
var options = '';
for (c in data) {
var cat = data[c];
options += "<option value='" + cat.CategoryId + "'>" + cat.CategoryName + "</option>";
}
$("#CategoryId").removeAttr('disabled').html(options);
} else {
$("#CategoryId").attr('disabled', true).html('');
}
});
});
Here is my controller action:
Function GetCategoriesByPage(ByVal PageId As Integer) As JsonResult
Dim categories As List(Of Models.WebCategoryLite) = _service.ListCategoriesByPageId(PageId)
Dim res As New JsonResult
res.Data = categories
Return res
End Function
Fiddler shows me that the JSON is being returned to the browser:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: Mon, 24 Aug 2009 19:43:53 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 1.0
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 246
Connection: Close
[{"CategoryID":1,"CategoryName":"Sample Category"},{"CategoryID":2,"CategoryName":"Another Sample"},{"CategoryID":3,"CategoryName":"Yet Another Sample"}]
No matter what browser I try this with, I get a "save file as" prompt.
I am running this from inside the Visual Studio 2008 IDE. What do I have to do to make this work, both in the IDE and from IIS?
Thanks in advance!