开发者

MVC - Passing additional information to the view

I am developing a website using Asp.Net MVC framework. We need to add general user information across a number of pages, in a simila开发者_开发技巧r 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?


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();


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
    }
}


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)
    {
      //...
    } 
  }
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜