ASP.NET MVC: How to covert an ActionResult to string?
Asked Answered
P

3

8

I would like to take an existing action method, render its return value to a string and ship it as a JSON for a response to an AJAX request.

To do this, I need to render an ActionResult to a string. How do i do this?

We have the opposite where we can convert a string to an ActionResult by using this.Content().

Update

The existing and 1st action method returns a type ActionResult but it really returns a ViewResult to respond to HTTP post request. I have a 2nd action method (my facade) that returns a JsonResult that responds to AJAX requests. I want this 2nd action method to use the 1st action method to render the HTML.

In the grand scheme of things, I want an ActionResult (generated from an action method) retrievable not only by a standard HTTP post, but also by an AJAX request via a facade action method (the 2nd action method). This way, I, as a developer, have the choice of using an HTTP Post or AJAX to retrieve the rendering of a page.

Sorry i tried to make this update as short as possible. Thanks.

Paling answered 3/1, 2011 at 15:30 Comment(1)
Surely ContentResult or JsonResult is what you want.Zinck
H
6

Are you looking for number 4 or 6 bellow?

Text extracted from here:

Understanding Action Results

A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.

The ASP.NET MVC framework supports several types of action results including:

  1. ViewResult - Represents HTML and markup.
  2. EmptyResult - Represents no result.
  3. RedirectResult - Represents a redirection to a new URL.
  4. JsonResult - Represents a JavaScript Object Notation result that can be used in an AJAX application.
  5. JavaScriptResult - Represents a JavaScript script.
  6. ContentResult - Represents a text result.
  7. FileContentResult - Represents a downloadable file (with the binary content).
  8. FilePathResult - Represents a downloadable file (with a path).
  9. FileStreamResult - Represents a downloadable file (with a file stream).

All of these action results inherit from the base ActionResult class.

Histiocyte answered 3/1, 2011 at 15:34 Comment(0)
G
1

Return it as a ContentResult rather than an ActionResult

I use something like

    public ContentResult Place(string person, string seat)
    {
        string jsonString = null;
        try
        {

            jsonString = AllocationLogic.PerformAllocation(person, seat);
        }
        catch {
            jsonString = AllocationLogic.RaiseError(timeout);
        }
        return Content(jsonString);
    }
Gyatt answered 3/1, 2011 at 15:37 Comment(2)
A JsonResult is likely an even better choice.Bambibambie
I think you are probably right...I may have another look at my code :-)Gyatt
B
1

Are you sure JsonResult isn't what you want? If you call the Json(object jsonObject) method that is defined in Controller, it will serialize jsonObject into JSON and return an appropriate response (with all the headers correctly set and all that). Generally JSON requests need to be POST, but you can configure it to allow GET too.

Bambibambie answered 3/1, 2011 at 15:37 Comment(2)
The existing action method returns an ActionResult but it really returns a ViewResult. I cannot return a JsonResult for the existing Action method but I would use JsonResult for the Action method that will ships the Json. There are two methods to take into consideration.Paling
But why would an AJAX request want an entire page? It would receive way too much info, the doctype, head, body, etc. Wouldn't an AJAX request just want a chunk of data? Have you considered using Request.IsAjaxRequest() to have one action handle both scenarios?Bambibambie

© 2022 - 2024 — McMap. All rights reserved.