1

Short version:

How does System.Web.MVC.Controller.View(object) work?

Long version:

I need to prepend my JSON results with an arbitrary string (Unparsable Curft).

The thing I'm unsure of is how I can modify the ViewResult within the ASP.NET MVC "pipeline". I've read the MSDN docs on the subject, but I'm still unclear on how to approach this.

  • How does View(Object) return a JSON string in this case?

Controller Sample

    [GridAction]
    public ActionResult _SelectBatchEditingGrid(int? id)
    {
        // GridModel is of type IEnumerable if that matters.
        // More info on the GridModel type see: http://www.telerik.com/help/aspnet-mvc/t_telerik_web_mvc_gridmodel_1.html

        return View(new GridModel(SessionProductRepository.All())
    }

View Sample

   <% Html.Telerik().ScriptRegistrar()
           .OnDocumentReady(() =>
           {%>
           /* Protect from setter-property hacks; see https://stackoverflow.com/a/3147804/328397  */
           $.ajaxSetup({
    converters: {
        "text cleanedjson": function(data) {
            var jsonString = data.replace("throw 1; <dont be evil> ", "");
            return $.parseJSON(jsonString);
                 } // End function
           } // end conveter
}); // end ajaxsetup
  • What is the best approach to prepend a string to my JSON data, via the return View(someObject) method?

Ideally, adding an attribute to each relevant method might be the best way to go, but I can handle this via reflection once I understand how to modify the JSON result.

Community
  • 1
  • 1
makerofthings7
  • 60,103
  • 53
  • 215
  • 448

1 Answers1

0

JSON is just a string so you can manipulate it any way you want before you return it. Not sure what is requesting the JSON, but if it is just an AJAX type request using something like JQuery Post you can do something like this in your controller. You do not have to return a View in your action methods.

    [HttpPost]
    public string GetSomeJson()
    {
        MyObject mo = new MyObject();
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(mo);
        string unparsableJson = unparsableString + json;
        return unparsableJson;
    }
Kevin Junghans
  • 17,475
  • 4
  • 45
  • 62
  • Using strings is easy... however the application I'm working with has a method signature of `public ActionResult ReturnSomeJSON`. The data is returned by sending a [Telerik GridModel](http://www.telerik.com/help/aspnet-mvc/t_telerik_web_mvc_gridmodel_1.html) class to `Controller.View(Object)` which does some magic to convert that `IEnumerable` into an `ActionResult`. I'm pretty sure `ToString()` is not being called in my case. How does MVC's view take an View(object) and cast it into a ActionResult which is a JSON string in this case? – makerofthings7 Feb 20 '12 at 19:51
  • Not sure I understand your question then. Showing some of the relevant code for the controller and view would help. – Kevin Junghans Feb 20 '12 at 20:10
  • Why do you think this ActionResult is returning JSON? ActionResult is just the View Model used to render the View by the view engine in this case. – Kevin Junghans Feb 20 '12 at 21:21
  • I did a fiddler trace when the Telerik Grid called the method and visually inspected it. Telerik does interesting things in general, and I'm hoping to learn from it. – makerofthings7 Feb 20 '12 at 21:28
  • I think I get it now after looking into the GridAction attribute. This is a Telerik specific attribute that helps Telerik do their magic to handle Ajax requests and convert the view model into JSON to be rendered in the Grid. I do not think this is so much an MVC specific question as it is a Telerik question on how they handle this. Might be black box you do not have access to. – Kevin Junghans Feb 20 '12 at 21:33