以字符串形式返回其他操作结果

发布于 2024-10-22 04:54:52 字数 632 浏览 0 评论 0原文

在我的 MVC 网站中,我正在创建一个小型论坛。对于单个帖子,我在“PostController”中渲染“Single(Post post)”操作,如下所示

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

另外,当用户回复帖子时,我将回复作为 ajax 请求发送到我的“CreatePost”操作,然后返回“Single”视图作为如下操作的结果,

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

当我确实喜欢仅渲染视图时,“单个”操作主体中的代码不会被执行。

最好的方法是什么?

另外,我想将“单个”操作结果作为 JsonObject 中的字符串返回,如下所示

return Json(new{IsSuccess = true; Content= /*HERE I NEED Single actions result*/});

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*/});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城旧梦 2024-10-29 04:54:52

您可以使用类似的东西,但要非常小心。它实际上可能会导致严重的可追溯错误(例如,当您忘记在 Single 方法中显式设置视图名称时)。

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);
}

更干净的解决方案是像从标准表单发布一样 - 重定向(XMLHttpRequest 将遵循它)

为了返回包装在 json 中的 ajax 视图,我使用以下类

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()
            }));
        }
    }
}

它可能不是最好的解决方案,但它工作得很好。请注意,您需要手动设置 View、ViewData.Model、ViewData、MasterName 和 TempData 属性。

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.

复古式 2024-10-29 04:54:52

我的建议:

  • 通过 Ajax 发布您的论坛回复(以及任何选项)。
  • 使用以下方法返回 JSONResult:ASP MVC 查看 JSON 内容 进行渲染你的内容。
  • 在 ajax 调用的 OnSuccess 处理程序中,检查 IsSuccess 是否为 true。如果成功,使用 JQuery 将内容附加到适当的容器

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文