title: Asp.net MVC Model for view and Layout
body: <p>I've been trying to find a good way to handle the Models of our Asp.net MVC websites when having common properties for all the pages. These properties are to be displayed in the Layout (Master Page). I'm using a "BaseModel" class that holds those properties and my Layout use this BaseModel as its model. </p>
  
  <p>Every other model inherits from that BaseModel and each has specific properties relative to the view it represents. As you might have guessed, my Models are actually View Models even if that's not quite relevant here.</p>
  
  <p>I have tried different ways to initialize the BaseModel values</p>
  
  <ol>
  <li>By "hand" in every view</li>
  <li>Having a base controller that has an Initialize virtual method to do it (so specific controller can implement specific common behavior for exemple)</li>
  <li>Having a base controlelr that override OnActionExecuting to call the Initialize method</li>
  <li>Using a helper class to do it outside of the controller</li>
  <li>Using a Model Factory </li>
  </ol>
  
  <p>But none of those really appeal to me:</p>
  
  <ol>
  <li>Seems obvious to me, but DRY is one reason enough to justify that (actually I never tried that solution at all, I'm just putting it to be able to loop on that point in the last point).</li>
  <li>I don't like that one because it means that whenever a new Controller is added, you need to know that it has to inherit from the BaseController and that you need to call the Initialize method, not to mention that if your controller has overriden the base one, to call the base anyway to maintain the values.</li>
  <li>see next point </li>
  <li>and 3. are a variation on the same topic but that doesn't really help with the issues of the second solution.</li>
  <li>My favorite so far, but now I have to pass a few more variables to set those values. I like it for the inversion of dependence. But then if I want to provide values from the session, I need to pass them explicitly for exemple, then I'm back to square one as I have to provide them by hand (being references or through an interface of any kind)</li>
  </ol>
  
  <p>Of course, (almost) all of those solutions work, but I'm looking for a better way to do it.</p>
  
  <p>While typing this question, I found maybe a new path, the <a href="http://en.wikipedia.org/wiki/Builder_pattern" rel="nofollow" title="Builder Pattern">builder pattern</a> that might also do, but implementations can become quickly a burden too, as we can have dozens of views and controllers. </p>
  
  <p>I'll gladly take any serious recommandation/hint/advice/patterns/suggestion !</p>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks to @EBarr I came up with another solution, using an ActionFilterAttribute (not production code, did it in 5 minutes): </p>
  
  <pre><code>public class ModelAttribute : ActionFilterAttribute
  {
      public Type ModelType { get; private set; }
  
      public ModelAttribute(string typeName) : this(Type.GetType(typeName)) { }
  
      public ModelAttribute(Type modelType)
      {
          if(modelType == null) { throw new ArgumentNullException("modelType"); }
  
          ModelType = modelType;
          if (!typeof(BaseModel).IsAssignableFrom(ModelType))
          {
              throw new ArgumentException("model type should inherit BaseModel");
          }
      }
  
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          var model = ModelFactory.GetModel(ModelType);
  
          var foo = filterContext.RequestContext.HttpContext.Session["foo"] as Foo;
  
          model.Foo = foo;
          model.Bar = somevalue;
  
          filterContext.Controller.TempData["model"] = model;
      } 
  }
  </code></pre>
  
  <p>Calling it is then really simple:</p>
  
  <pre><code>[Model(typeof(HomeModel))]
  public ActionResult Index()
  {
      var homeModel = TempData["model"] as HomeModel;
  
      // Add View Specific stuff
  
      return View(homeModel);
  }
  </code></pre>
  
  <p>And it gives me the best of every world. The only drawback is to find a proper way to passe the model back to the action. </p>
  
  <p>Here it's done using the TempData object, but I also consider updating the model that one can find in the ActionParameters.</p>
  
  <p>I'm still taking  any serious recommandation/hint/advice/patterns/suggestion for that, or the previous points.</p>