views:

268

answers:

4

As a learning exercise, I want to migrate an existing flash application to ASP.NET MVC. It has around 20 forms, and although it's mostly linear, there is some degree of alternate flows based on user decisions or data returned.

Could anyone point me in the right direction of how a controller would handle this? I don't want my views to have to figure out where they go to next.

Update

I think I may not be understanding the correct way of building this. I saw each controller as taking care of a different section of the app, and having a main controller being responsible for workflow.

If this is not the approach I should be taking, what is the best way of doing this?

Update 2

Would Areas in ASP.NET MVC 2 take care of this sectioning of the app? I really don't like the idea of having too many actions in one controller...

A: 

ScottGu's blog has a quite detailed tutorial that may be helpful for you.

Konamiman
A: 

When a form posts to a controller action it is the controller action to decide what to do with the posted results and which view to render next or redirect:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult HandleFormSubmission(FormCollection col)
{
    // do something with posted data
    // redirect to /someOtherController/someOtherAction
    // which could show some other form
    return RedirectToAction("someOtherAction", "someOtherController");
}
Darin Dimitrov
Thanks for the quick response. I had been thinking that as there are so many forms, that each should have its own controller, but it sounds like only one controller will be necessary? Each action could then hand over responsibility to the controller to decide where to go next?Wouldn't this make a potentially huge controller?
Paul
@Paul, yes it does make for a potentially large controller, but as with all things OO you can always refactor, perhaps by adding a service layer behind the controller.
Robert Harvey
A: 

I was struggling with the same problem (using Windows Workflow Foundation with ASP.NET MVC) and I blogged about it here

Maybe you'll find it helpful, sorry for pushing my own link

Jaco Pretorius
+1  A: 

In general:

Controllers are usually a collection of actions that deal with a logically coherent chunk of the application ( hence why you often see UserController / OrderController etc etc ).

MVC applications should be built using PRG ( post - redirect - get ) which means you will have 2 actions for each form, one that will display the form and the second, with the same name but decorated with [AcceptPost], that will process the form and redirect the user to the appropriate location based on the result.

The simplest way to learn how this works and migrate your app over is model each form as a simple dto with no logic, build a view for each form and 2 actions.

Once you have the logic working in the controller, you may want to migrate it out into some form of service that can be injected into the controller.

Specifically for your workflows:

Each workflow should probably have it's own controller. It may be useful to model them using some form of state pattern ( depending on the complexity of the workflow ), and providing a result from each state transition that your controller can translate into a redirect to the next step in the workflow.

Neal
You won't always need two actions for each view. If you are displaying a list (with links), you only need a GET. If you click on one of the links and display the details, you still only need a GET. If you click the edit link on the details view, now you need a GET and a POST.
Robert Harvey
The line says "2 actions for each form", not 2 actions for each view
Neal