views:

111

answers:

2

Lets say I've a website that lists Persons, and each Person has multiple properties, some one-to-one (name, address), some one-to-many (favorite colors, bank-accounts etc).

In my business layer it's nicely hierarchically organized.

How do I organize this is my controllers and views? Should I have a PersonsController, FavoriteColorsController etc? all residing in the same namespace and folder? Or should I have ony a PersonsController with many actions, such as IndexFavouriteColors, UpdateFavoriteColor etc. Both of the options are not quite it. The first one doesn't show that FavoriteColors is a child of Person and can only be used in the context of a person. The second one will create a huge PersonController.

The same thing with the views of course. The nicest would be to have

  • Views/Persons/index.aspx
  • Views/Persons/details.aspx
  • Views/Persons/ etc.
  • Views/Persons/FavoriteColors/index.aspx
  • Views/Persons/FavoriteColors/details.aspx

In this example I gave only a few one-to-many properties to the Person, but actually there are many (10+), so that increases the need for clarity.

Thanks.

A: 
PersonsController.cs

public ActionResult Index()
{
  return View();
}
public ActionResult Details()
{
  return View();
}
public ActionResult Info()
{
  return View();
}

....

Right click in each methods and generate views

In Views folder in a result you should have

Views
----- Persons
-------------Index
-------------Details
-------------Info
-------------....
omoto
The person itself is not the problem. Its the division of the views that are "children" of the current view.I want to create a view folder structure that represent the route structure like :/Person/ /Person/{id}/details/Person/{id}/FavoriteColors//Person/{id}/FavoriteColors/{id}/details
Gidon
+3  A: 

Actually I found the answer in "Areas", which is supposedly supported by Rails, but not by MVC, though there are private implementations:

Grouping Controllers with ASP.NET MVC
Creating MVC "Areas" as Subfolders under Views

Gidon
The second link got mangled somehow. It looks like there's an extra trailing slash.
AaronSieb