控制器和上一个视图

时间:2009-08-07 16:43:39

标签: asp.net asp.net-mvc controller

现在,我在MVC应用程序的Index页面中只有一个与POST关联的动作。但是我想要一个处理所有页面帖子和POST Handler操作的通用处理程序操作,我想知道我刚刚来自的视图。我想要的是下面:任何想法?

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult GenericPostHandler(NewAccountInfo info)
    {
        try
        {
            string currentview = //how to get View for which to handle the POST?
            Persist();

            return RedirectToAction(StepManager.GetNextStep(currentView));
        }
        catch
        {
            return View();
        }
    }

2 个答案:

答案 0 :(得分:1)

我认为您可以编写将所有请求转发到一个特定控制器中的单个操作的路由,但这将对所有HTTP命令激活,无论是GET还是POST或其他。

你想要实现的是非常违背MVC的精神。你能否告诉我们这个要求背后的想法是什么?

我会猜测。你希望在每个帖子上进行某种预处理,对吧?可能是授权检查,活动日志等。如果是这样,您可以实现自己的ActionFilter并使用此属性装饰所有控制器。然后所有调用都将被此过滤器截获,您可以随意执行任何操作 - 然后将请求传递到其正常处理程序(操作)或将其路由到其他位置。

答案 1 :(得分:0)

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GenericPostHandler(NewAccountInfo info, string fromViewName)
{
    ...
}

在视图中:

<% var actionUrl = Url.RouteUrl(new {
    controller = "yourcontroller",
    action = "genericposthandler",
    fromviewname = "whereyoucamefrom"
}); %>

<form action="<%= actionUrl %>" method="post">
    ...
</form>

生成类似/yourcontroller/genericposthandler?fromviewname=whereyoucamefrom

的网址