Creating meaningful routes in wizard style ASP.NET MVC form

Posted by R0MANARMY on Stack Overflow See other posts from Stack Overflow or by R0MANARMY
Published on 2010-03-17T22:09:58Z Indexed on 2010/03/17 22:11 UTC
Read the original article Hit count: 436

I apologize in advance for a long question, figured better have a bit more information than not enough.

I'm working on an application with a fairly complex form (~100 fields on it). In order to make the UI a little more presentable the fields are organized into regions and split across multiple (~10) tabs (not unlike this, but each tab does a submit/redirect to next tab). This large input form can also be in one of 3 views (read only, editable, print friendly).

The form represents a large domain object (let's call it Foo). I have a controller for said domain object (FooController). It makes sense to me to have the controller be responsible for all the CRUD related operations. Here are the problems I'm having trouble figuring out.

Goals:

  1. I'd like to keep to conventions so that
    Foo/Create creates a new record
    Foo/Delete deletes a record
    Foo/Edit/{foo_id} takes you to the first tab of the form
    ...etc

  2. I'd like to be able to not repeat the data access code such that I can have
    Foo/Edit/{foo_id}/tab1
    Foo/View/{foo_id}/tab1
    Foo/Print/{foo_id}tab1
    ...etc
    use the same data access code to get the data and just specify which view to use to render it.

My current implementation has a massive FooController with Create, Delete, Tab1, Tab2, etc actions. Tab actions are split out into separate files for organization (using partial classes, which may or may not be abuse of partial classes).

Problem I'm running into is how to organize my controller(s) and routes to make that happen. I have the default route {controller}/{action}/{id} Which handles goal 1 properly but doesn't quite play nice with goal 2. I tried to address goal 2 by defining extra routes like so:

routes.MapRoute(
    "FooEdit",
    "Foo/Edit/{id}/{action}",
    new { controller = "Foo", action = "Tab1", mode = "Edit", id = (string)null }
);
routes.MapRoute(
    "FooView",
    "Foo/View/{id}/{action}",
    new { controller = "Foo", action = "Tab1", mode = "View", id = (string)null }
);
routes.MapRoute(
    "FooPrint",
    "Foo/Print/{id}/{action}",
    new { controller = "Foo", action = "Tab1", mode = "Print", id = (string)null }
);

However defining these extra routes causes the Url.Action to generate routs like Foo/Edit/Create instead of Foo/Create. That leads me to believe I designed something very very wrong, but this is my first attempt an asp.net mvc project and I don't know any better.

Any advice with this particular situation would be awesome, but feedback on design in similar projects is welcome.

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about asp.net-mvc-routing