在部分视图中保存后重定向 [英] Redirect after save in partial views

查看:65
本文介绍了在部分视图中保存后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,要求是在将模型成功保存到ASP.NET MVC控制器中后重定向到另一个页面:

Say the requirement is to redirect to another page after successfully saving a model in an ASP.NET MVC controller:

[HttpPost]
public ActionResult Index(ViewModel viewModel)
{
   if (ModelState.IsValid)
   {
        // Do save;

        return RedirectToAction("whatever"); // <--- here's the problem 
    }

    // display validation errors and so
    return View(viewModel);
}

除非将控制器呈现为子动作",否则这将很好地工作:

This would work fine unless the controller was rendered as a Child Action:

@{
    Layout = "my layout";

    Html.RenderAction("something else, a view for example");
    Html.RenderAction("Index action of the above controller"); // <----
}

在这种情况下,RedirectResult类将检查并确定上下文是子操作,并会引发异常:不允许子操作执行重定向操作".

In this case RedirectResult class would check and see that the context is a child action and would throw the exception: "Child actions are not allowed to perform redirect actions".

我了解到写入响应流已经在进行中,并且无法在此处进行重定向,但是即使在控制器中服务器端操作之后,该操作也必须能够重定向到其他页面,即使该操作是子操作.我们使用Layouts和RenderActions重用页面设计的共享部分.

I understand that writing to Response stream is already in progress and a redirect cannot take place here but nevertheless one has to be able to redirect to other pages after a server-side action in a controller even if that action is a child action. We use Layouts and RenderActions to reuse the shared parts of the page design.

您将如何在这样的控制器中实现这样的重定向?

How would you implement such a redirect in such a controller?

主要目标是重用执行特定工作的视图/控制器,并将其拆分为逻辑概念,例如显示一些数据或提供编辑表单.我在这里的方法是使用RenderAction将结果呈现到容器中.容器视图(主页的索引"操作)充当传统的asp.net页面,其布局视图作为母版页面,并且编辑和视图控制器/视图等效于用户控件(模块).问题在于,在向其中写入一些内容之后,无法重定向响应:

The main goal is to reuse View/Controllers that do a specific job and split them to logical concepts like displaying some data or providing an edit form. My approach here is to use RenderAction to render their result into a container. Container view (main page's Index action) acts as a traditional asp.net Page, its Layout view as a Master page and edit and view controller/views are equivalent to User Controls (modules). The problem is there is no way to Redirect the Response after something has been written to it:

推荐答案

我将提交新的答案,以保持环境整洁.
正常的mvc流如下所示:
Http命令到达一个充当合唱大师的控制器(又名控制器),并调用多个逻辑容器(例如services/commandHandlers):

I'll submit a new answer in an attempt to keep things clean.
A normal mvc flow goes like this :
Http command reaches 1 controller which acts as a choirmaster(aka controller) and calls several logic containers(e.g services / commandHandlers) :

public ActionResult Index(){
  var data = _yourService.FetchData();
  return View(data);
}

此控制器呈现1个可以包含多个局部视图的视图

This controller renders 1 view which can have multiple partials

@{
    Layout = "my layout";
}
<p>Some html</p>
Html.RenderPartial("A shared partial");
Html.RenderPartial("shared\yourUserControl", Model.PropertyOrSomething);

如果部分包含太多无法生成的逻辑,则可以添加RenderAction或创建htmlHelper扩展.
但是,我认为这些都不应该能够控制您的请求流程,保存或可以重定向的内容都不能从视图内部调用.
我假设您想如此严重地重用控制器中的代码,因为它变得越来越大.
我的建议是通过尽可能多地委派尽可能多的逻辑来尝试尽量清除该控制器.
只要查看您的控制器方法5秒钟,您就应该知道此操作将执行的操作,如果不是这种情况,请重构它! :)

If the partial contains too much logic to generate you could add a RenderAction or create an htmlHelper extension.
But neither of these should be able to control the flow of your request, a save or something that can redirect should in my opinion never be called from inside a view.
I assume you want to reuse the code in your controller so badly because it is getting quite big.
My advice would be to try and clear that controller up as much as possible by delegating as much of the logic upwards as you can.
Just by looking at your controller method for 5 seconds you should get an idea of what this action will do, if that's not the case : refactor it ! :)

这篇关于在部分视图中保存后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆