0

In my MVC website, I am creating a small forum. For a single post I am rendering my "Single(Post post)" action in my "PostController" like below

<% Html.RenderAction<PostController>(p => p.Single(comment)); %>

Also When a user reply a post I am sending reply as an ajax request to my "CreatePost" action then return "Single" view as result of this action like below

public ActionResult CreatePostForForum(Post post)
{
    //Saving post to DB
    return View("Single", postViewData);
}

When I do like that only the view is being rendered, Codes in "Single" Actions body isn't beig executed.

What is the best way to do this?

Also I want to return "Single" action result as string in my JsonObject like below

return Json(new{IsSuccess = true; Content= /*HERE I NEED Single actions result*/});
The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
Yucel
  • 2,603
  • 5
  • 28
  • 40

2 Answers2

0

You can use something like this, but be very careful with this. It can actually cause badly traceable errors (for example when you forget to explicitly set view name in Single method).

public ActionResult Single(PostModel model) {
    // it is important to explicitly define which view we should use
    return View("Single", model);
}

public ActionResult Create(PostModel model) {

    // .. save to database ..

    return Single(model);
}

Cleaner solution would be to do the same as if it was post from standard form - redirect (XMLHttpRequest will follow it)

For returning ajax views wrapped in json I use following class

public class AjaxViewResult : ViewResult
{
    public AjaxViewResult()
    {

    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (!context.HttpContext.Request.IsAjaxRequest())
        {
            base.ExecuteResult(context);
            return;
        }

        var response = context.HttpContext.Response;

        response.ContentType = "application/json";

        using (var writer = new StringWriter())
        {
            var oldWriter = response.Output;
            response.Output = writer;
            try
            {
                base.ExecuteResult(context);
            }
            finally
            {
                response.Output = oldWriter;
            }

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            response.Write(serializer.Serialize(new
            {
                action = "replace",
                html = writer.ToString()
            }));
        }
    }
}

It is probably not the best solution, but it works quite well. Note that you will need to manually set View, ViewData.Model, ViewData, MasterName and TempData properties.

Lukáš Novotný
  • 9,012
  • 3
  • 38
  • 46
0

My recommendation:

  • Post your forum reply (and whatever options) via Ajax.
  • Return your JSONResult, using this method: ASP MVC View Content as JSON to render your content.
  • In the OnSuccess handler of your ajax call, check if IsSuccess is true. If successful, append the content to the appropriate container using JQuery
Community
  • 1
  • 1
Keith
  • 5,311
  • 3
  • 34
  • 50