views:

573

answers:

1

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!

+1  A: 

Just set Content-type to "text/plain":

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
    res.ContentType = "text/plain"
    Return res

End Function

If it doesn't work, you can subclass JsonResult and override ExecuteResult method:

public class myOwnJsonResul: JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        base.ExecuteResult(context);
        context.HttpContext.Response.ContentType = "text/plain";
    }
}
Cleiton
*I wrote second example in C# because I don't remember the VB syntaxe
Cleiton
Well, I tried HttpContext.Response.ContentType = "text/plain"and Fiddler still showed the content type as "Application/JSON."Then I set the JSONResult object's "ContentType" property to "text/plain," and my original aspx page was replaced by the JSON result displayed in the browser page as plain text.
camainc
that second result is the correct behaviour.
DDaviesBrackett
@camainc, isnt the second result what you want? If it ins't, you will have to clarify your question.
Cleiton
No, the result is supposed to be consumed by the jquery ajax success function.I have used jQuery to make ajax calls with "regular" asp.net, and it never exhibited this type of behavior.
camainc
I set res.ContentType = "application/json; charset=utf-8" and it worked.
camainc
It is strange because "application/json; charset=utf-8" was what you are sending, anyway great it worked.
Cleiton