views:

45

answers:

2

Right now, I have an action associated with just the POST from the Index page in my MVC app. But I want a generic handler action that handles all page posts and within the POST Handler action, I want to know the view that I just came from. What I want is below: Any ideas?

    [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();
        }
    }
+1  A: 

I suppose you could write routes that will forward all requests to a single action in one particular controller but this will then be active for all HTTP commands, either GET or POST or otherwise.

What you are trying to achieve is very much against the spirit of MVC. Could you tell us probably what is the idea behind this requirement?

I will try to guess. You wish to perform some kind of preprocessing on each post, right? Maybe an authorization check, activity log etc. If so, you could implement your own ActionFilter and decorate with this attribute all of your controllers. Then all calls will be intercepted by this filter and you can do there whatever you need - then pass the request through to its normal handler (action) or route it elsewhere.

Developer Art
I am building a workflow. Basically, by know what view you came from, I can tell you what view to go to. How is this against the spirit of MVC though? I'm still using a controller to do the action routing, I'm just trying to reduce duplicate code and make things more generic. Thanks.
Crios
A: 
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GenericPostHandler(NewAccountInfo info, string fromViewName)
{
    ...
}

In Views:

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

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

produces url like /yourcontroller/genericposthandler?fromviewname=whereyoucamefrom

eu-ge-ne