title: Multi Tenant MVC 3 App with View Model Problem
body: <p>I am writing an app that utilizes multi-tenancy as outlined by <a href="http://weblogs.asp.net/zowens/archive/2010/05/26/multi-tenant-asp-net-mvc-introduction.aspx" rel="nofollow">Zack Owens</a>.</p>
  
  <p>In Zack's example he has separate c# class libraries to store the controllers and views for each tenant.  Since I am not using the Spark View engine which has compiled views this is not an option.  Instead I have created a separate MVC3 App Project for each tenant and in there I am storing the content, controllers, views, etc.</p>
  
  <p>I have a main MVC App Project (which is the startup app) with a custom controller factory which scans the assemblies similar to Zacks example and sets up dependency injection using structure map.  I have a view engine that uses the controller context to grab the correct view. If it can't find a view or dependency in the tenants app it will use the view or dependency from our main MVC App Project.  Everything is working awesome except when it comes to viewmodels.</p>
  
  <p>Most (if not all) of our views are passed a view model.  I would like the View Model class to reside in the Tenant App Project and not in the Main App Project so tenants can have custom view models to go with their custom views.</p>
  
  <p>It seems like it would work but I keep getting Errors.</p>
  
  <p><strong>Scenario #1</strong> - The default View Models reside in the main MVC App Project.  The Tenant App has a reference to the Main App and therefore can use the default View Models. No Problems.  Now when I want to add a View Model or Override a View Model, I create the class and put it in the Tenant App.  The problem is when you call the page, the Main MVC App can't find the new View Model because there is no reference from the Main App to the Tenant App and their can't be due to circular reference issues.</p>
  
  <p><strong>Scenario #2</strong> - I remove the reference from the Tenant App to the Main App.  I then copy the default view models from the Main App to the Tenant App, change the namespaces, and create a reference from the Main App to the Tenant App so the view model classes are accessible to the Main App.</p>
  
  <p>I then run it and get the following error:</p>
  
  <p><strong>The model item passed into the dictionary is of type 'Sample.Models.Login', but this dictionary requires a model item of type 'Sample.Models.Login'.</strong></p>
  
  <p>The error makes sense, but not really. I only have one class named Sample.Models.Login</p>
  
  <p>I have some assumptions on why this might be doing this but I can't get it figured out.  Any tips or advice would be greatly appreciated.</p>
  
  <p>Thank You</p>