sterix24
sterix24

Reputation: 2400

How should I structure my mvc 3 app in this situation?

I'm trying to make a simple website using mvc but I'm a little confused about how to structure everything. Are there any guidelines for this sort of thing?

What I'm having difficulty with is knowing how to deal with a lot of nested pages, and how to use ViewModels, or rather when

I've built a site in MVC before but it was incredibly simple: there was only about 5 pages total so it wasn't difficult to build. In my current situation, I'm trying to build a site that has a number of nested pages and I wonder how I structure this.

For example, if I had a structure something like this:

                                        Home
                                          |
                        About -------- Contact---------- Products
                          |
                          |
           The Board -- What we do
             |
             |
 person1 -- person2 -- person3

How should I set up my site structure?

My gut feeling is that I should have an AboutController, a ContactController and a ProductsController. I'd then have two ActionResults called 'TheBoard' and 'WhatWeDo' in the AboutController but then how do I handle the nested pages of 'TheBoard'? What if I had even more pages nested underneath this? Would I have nested Controllers?

Also, even though this dummy site doesn't require it, what if I wanted to share ViewModels? I'm assuming a ViewModel can be shared across pages (assuming the same functionality is required). Or is this bad practice? Should a new ViewModel be created for each page?

Apologies if I haven't explained this very well. Basically what I'm looking for is any resources on how to structure an MVC 3 web app that contains a lot of pages. Are there any basic principles that should be followed? I've followed most of the tutorials on the asp.net site but there weren't really any guidelines on best practices.

Any ideas?

Thanks

EDIT: Just to clarify, if I was building this site using webforms, I'd probably have a default.aspx page with About, Contact, Products folders and any nested pages/folders within. I suppose I'm trying to figure out how I'd do this in MVC?

Upvotes: 0

Views: 151

Answers (2)

danludwig
danludwig

Reputation: 47375

I agree with Darin that you need to describe the functionality of the site. If all you are trying to do is manage content, then you might be better off using an out of the box CMS like Orchard. You can use a CMS like that to create any arbitrary structure of linked pages.

Your comment "But what if I wanted to go one level further?" sounds to me like the requirements for this project are not yet clear. You can always expand and refactor later. For now, just implement the requirements that have been set by your customer.

Initially, make sure you are "building the right it" before turning your attention to "building it right".

Reply to comments

It sounds like you are worried about navigation and URL structure. In MVC, your URL structure can be completely independent of your controller organization. There is no notion of "controls" in MVC. There are HTML helpers, but no controls in the webforms sense of server controls.

If for example if you wanted /board/members/jim/bio/resume/company1/edit, that is entirely possible. It does not matter which controller the ResumeCompanyEdit action is in, because you can define custom routes to link the URL you want to this action.

As for breadcrumbs, you can create these manually, or write a custom HtmlHelper to render out the breadcrumbs for you, based on your custom URL schema.

Upvotes: 2

reach4thelasers
reach4thelasers

Reputation: 26899

I would have an AboutController, a ContactController, a ProductController as you mention

The AboutController could have an Action Person.

Set up a route in global.asax as follows:

        routes.MapRoute(
            "AboutPerson", // Route name
            "about/board/{personName}", // URL with parameters
            new { controller = "About", action = "Person"} 
        );

the Person action should look like this:

    public ActionResult Person(string personName) {
         // Code to get person data from DB - if required
         var person = db.GetPerson(personName);
         return View(person);
}

If the pages are static, just have different views and check the personName and return the relevant view: e.g.

return View("BoardMember1");

Where BoardMember1.cshtml / aspx is a View within the Views/About Directory

Upvotes: 1

Related Questions