views:

530

answers:

7

We're evaluating asp.net MVC and are looking for some more complicated examples over and above NerdDinner.

Specifically, in a more complex web app, I might have a navigation bar (including primary nav, a search box and a log-in status display), a main content area, a sub-content area (including related content) and a footer. In MVC a controller returns a ViewModel (not a View if I'm thinking that I want to de-couple my Controller from my View) - would my ViewModel have to have properties to cover each and every aspect of the "page" I am aiming to render as output?

If this is unclear, I may be able to re-word my question.

BTW - I know this site is built using MVC. I'm after downloadable examples.

Thanks in advance.

+2  A: 

You are welcome to take a look at good.codeplex.com

It has much of what you seek above but there is work to be done! However, after you've looked I'd be happy to field questions on it here or over on codeplex.

This is what mygoodpoints.org is currently running on.

dove
Aside: It'd be great if you had some JS to animate the water pump when you clicked on it. :) Otherwise, an absolutely brilliant site!
Dan Atkinson
+5  A: 

Take a look at CodeCampServer.

Edit: With reference to your query about view models, this isn't the perfect answer to it but thought I'd draw attention to AutoMapper (used by CodeCampServer) which can assist with auto mapping data between models and view models which is a real time saver. Also worth considering the concept of Input Builders (there's some available with MVCContrib and also some in ASP.NET MVC 2) which will also reduce the amount of data you have to pass into a view by encapsulating common functionality across the board.

There's a good video on the ASP.NET MVC 2 offering here: http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-ASPNET-MVC-2-Preview-1-with-Phil-Haack-and-Virtual-Scott/.

Luke Bennett
+1 they've recently put a good effort into improving codeCampServer as well. It's good to see it is active. It is the code companion to the Mvc in Action book by Manning.
dove
Call me thick by all means, but can you explain how I go about obtaining the source for the CodeCampServer app? There is no downloads tab!
Paul Suart
You will need to use a Subversion client like TortoiseSVN (www.tortoisesvn.org) to download the code - details are on the Source tab.
Luke Bennett
A: 

would my ViewModel have to have properties to cover each and every aspect of the "page" I am aiming to render as output?

Yes. There is another option with RenderAction, but apart from that a ViewModel in generall is often big and you have to find a good way to fill it. I admit this sounds like a trouble spot first.

Malcolm Frexner
A: 

AtomSite is a blog engine written using ASP.NET MVC

CmdrTallen
Is this site written in asp.net MVC? It seems a little "non-standard".
Paul Suart
I've downloaded it. it is :)
TimLeung
A: 

As far as I know, a Controller directly returns a View and can pass data to the View using either the ViewData or the Context.

The former is just a loose bag of various bits of data, whereas the latter is a specific type.

The ViewModel would be passed to the View as the Context (and the mark-up of the View would be strongly-typed to the type of ViewModel it expects).

That's my 2c worth :) Hope that helped-- sorry I could not include any downloadable examples.

Umar Farooq Khawaja
A: 

To automatically pass data to all views, you can make your own controller class and use that:

Example

    public class MyController : Controller

{
    private User _CurrentUser;

    public User CurrentUser
    {
        get
        {
            if (_CurrentUser == null)
                _CurrentUser = (User)Session["CurrentUser"];
            return _CurrentUser;
        }
        set
        {
            _CurrentUser = value;
            Session["CurrentUser"] = _CurrentUser;
        }
    }

    /// <summary>
    /// Use this override to pass data to all views automatically
    /// </summary>
    /// <param name="context"></param>
    protected override void OnActionExecuted(ActionExecutedContext context) 
    {
        base.OnActionExecuted(context);

        if (context.Result is ViewResult) 
        {
            ViewData["CurrentUser"] = CurrentUser;
        }
    }
    }
qui
But this gets tying very quickly over 100s of pages and dozens of "possible" sidebar and navbar widgets. You should check out Html.RenderAction to handle a lot of this (bing it).
eduncan911
+3  A: 

Here ya go:

<% Html.RenderAction<LayoutController>(c => c.SearchBox()); %>
<% Html.RenderAction<LayoutController>(c => c.NavBox(Model)); %>

Put these in your masterpages, or on specific views for sidebar widgets, and abstract their logic away from your controller/viewmodel you are working on. They can even read the current RouteData (url/action) and ControllerContext (parameters/models), cause you are dealing with ambient values in these objects - and executing a full ActionMethod request!

I blogged about this little known secret here. I also blogged about where this is located, which is the ASP.NET 1.0 MVC Futures assembly that is a seperate add-on from Microsoft.

Steve Sanderson actually gives gives examples of complex logic and application building in a book I have called Pro ASP.NET MVC (shameless plug, I know, but it's what you are looking for in your question), where he actually uses the RenderAction! I made the blog post, before I even read the book so I am glad we are on the same page.

Actually, there are dozens of extensions and functionality that was developed by the ASP.NET MVC team that was left out of the ASP.NET MVC 1.0 project - most of which makes complex projects much more managable. This is why more complex examples (list above in most people's answers) have to use some type of custom ViewEngine, or some big hoop jumping with base controllers and custom controllers. I've looked at almost all of the open source versions listed above.

But what it comes down to is not looking at a complex example, but instead knowing the ways to implement the complex logic that you desire - such as your Navigation bar when all you have is a ViewModel in a single controller to deal with. It gets tiring very quickly having to bind your navbar to each and every ViewModel.

So, an example of these is the Html.RenderAction() extension (as I started off with) that allows you to move that more complex/abstracted logic off of the viewmodel/controller (where it is NOT even your concern), and place it in its own controller action where it belongs.

This littler baby has saved MVC for me, especally on large scale enterprise projects I am currently working on.

You can pass your viewmodel into the RenderAction, or just render anything like a Footer or Header area - and let the logic be contained within those actions where you can just fire and forget (write the RenderAction, and forget about any concern of what it does for a header or footer).

eduncan911
This isn't really a little known secret ;)
Alex
Yeah, I see that now. http://stackoverflow.com/search?q=Renderaction
eduncan911