3

I am developing a website using Asp.Net MVC framework. We need to add general user information across a number of pages, in a similar way to the reputation bar at the top of stack overflow.

I consider it important that there should be no overhead added to the creation of a controller method in order to add this extra information to the view. This excludes the option of passing this info in the ViewData object, or amending the ViewModels to accept the reputation field as it would result in every controller looking like this:

public ActionResult Index()
{
    ViewData["reputationScore"] = GetUsersReputation(userId);

    // main controller logic here
}

If this is used across 90% of the pages on a site, it could be quite time consuming to alter if we also wanted to display the users badge count.

I can think of 4 solutions to this

Use Master pages Retrieve the user reputation in the master page code behind, and put the mark up for the reputation in the master page mark up.

Disadvantages:

  1. This seems to be moving away from everything that MVC represents.
  2. I have been looking at moving to using alternative view engines (e.g. razor) I am not sure how well these will mix.
  3. It tends to restrict the layout of the reputation - hard to put it in the middle of the page.

Extend HtmlHelper to add a GetUsersReputation method This seems to be a slight violation of what the Html object should be used for - it is not just rendering output, but hitting the database. I can't think of any other significant issues with this other than the violation of the metaphor.

Override the System.Web.Mvc.ViewPage Overriding Viewpage to expose an object in addition to Html that can be used to call a selection of methods that access the database. Make sure that extension methods can be added in the same way as HtmlHelper, so that as and when new methods are needed it can be extended appropriately. This could allow us to write something like:

<% DataAccess.GetUsersReputation() %>

Create a base generic view model Rather than passing your view model straight to the view, wrap it in a base view model that can hold all the methods you need:

public ActionResult Index()
{
    MyViewModel viewCoreInfo = model.GetData();

    return View(new BaseViewModel<MyViewModel>(viewCoreInfo));
}

BaseViewModel can expose all the properties that you need for extra information required on the web page. e.g. UsersReputation (it could either query the database in the constructor, or load the data when the property is accessed). I think this maintains the MVC metaphor better, but is a little cumbersome.

  1. Has someone else come up with a better solution
  2. Which is best - if you have used them have they been effective /problematic?
tereško
  • 58,060
  • 25
  • 98
  • 150
Giles Smith
  • 1,952
  • 15
  • 17

5 Answers5

3

Why don't you let MyViewModel inherit BaseViewModel?

The you can use BaseController.OnResultExecuting to fill the common values:

protected override void OnResultExecuting(ResultExecutingContext ctx) {
    base.OnResultExecuting(ctx);
    var baseView = ctx.Result as BaseViewModel;
    if (baseView != null)
    {
        //assign values here
    }
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Thanks for the answer. I like the idea of overriding the OnResultExecuting method. My worry would be getting MyViewModel to inherit from BaseViewModel, and you would then introduce coupling between the 2 classes. If you decided later that another group of pages needed to display a friend count but you are still using MyViewModel for the main data, you would then need to allow BaseViewModel to impliment both friend and reputation methods. Where as with my solution you just change the BaseViewModel for FriendDataBaseViewModel. Thanks for your help - will look into OnResultExecuting now. – Giles Smith Feb 04 '11 at 15:05
  • Well. Create an interface called `IMasterPageData` or something. Let your `BaseViewModel` and `FriendDataBaseViewModel` implement it. Then just change the check on `OnResultExecuting` to `IMasterPageData` instead. – jgauffin Feb 04 '11 at 18:25
3

Since you mentioned the possibility of moving to the razor view engine, I think that THIS blog post by ScottGu might be of interest.

He discusses using 'sections' with razor that will alow you to create a section on your Layout("Masterpage") that you can load with addition content. Creating your SO style info bar in this manner would be clean and simple.

EDIT:

As far as rendering partial views as per the comments below...the new mvc music store tutorial has some examples.

In this case they are using Html.RenderAction. Html.RenderPartial is another option. The differences are outlned in this question but im sure there are other resources on the net.

Your controller would just return a partial view as such:

 return PartialView();
Community
  • 1
  • 1
stephen776
  • 9,134
  • 15
  • 74
  • 123
  • Sections is just as ContentPlaceHolders in Webforms engine. It doesn't really solve the problem that the OP has. The OP is wondering how the info that are going to be placed in views/sections/whatever should be transported from the controllers. – jgauffin Feb 04 '11 at 13:40
  • Thanks for the answer. I think this still presents the same problem, that although razor allows you to create the mark up with master pages, it is generally considered bad practice to write code in views other than display logic. It is certainly not the place to be putting data access methods - this is really the responsibility of the model/controller. However to prevent duplication I am looking to avoid having to call the reputation methods in every controller. – Giles Smith Feb 04 '11 at 16:01
  • @Giles I see what you mean. What if the section were to render a partialview calling its own controller/action or something along these lines? – stephen776 Feb 04 '11 at 17:07
  • @Stephen776 If that is possible, that sounds like a nice solution. Can you call a controller from within a partial view? Have you seen an example of this anywhere? – Giles Smith Feb 04 '11 at 17:31
  • @Giles Yes I believe its as simple as Html.RenderPartial("action", "controller", "other params(optional)"). I know I have a sample of this on my dev machine. I can post an example when I find it – stephen776 Feb 04 '11 at 17:55
  • @Giles updated answer above to give some more info. It is definitely possible and seems to me to be a clean solution – stephen776 Feb 04 '11 at 18:02
  • @Stephen776 That is exactly what I was after - assumed I was missing something somewhere. Thank you for helping out. – Giles Smith Feb 05 '11 at 10:49
1

You can create action filter, override OnResultExecuting, fill the common values and add it to your action/controller:

public class AddCommonData : ActionFilterAttribute 
{
  public override void OnResultExecuting(ResultExecutingContext filterContext)
  {
    ViewResult viewResult = filterContext.Result as ViewResult;
    if (viewResult != null)
    {
      //...
    } 
  }
}
MishaU
  • 708
  • 6
  • 14
0

I've done that and i made a base model class with all the information in common. All my other viewmodel class inherits from this one. But you have to populate in each controller these informations. This is the solution I've choosed.

alexl
  • 6,841
  • 3
  • 24
  • 29
0

You can create partial view with codebehind file where you'll populate partial view with data. It is a bit non-mvc way but it'll work.

jezzarax
  • 417
  • 2
  • 10