title: MVC Partial Views in Site.Master
body: <p>I am trying to render a partial view in my Site.Master file but I keep on getting an error (Object reference not set to an instance of an object).  If I access the view directly, I get the info that I need, but if I access the Index View from the HomeController, I get an error.    The partial view will display the logged in user, department, and role.  This is my code.</p>
  
  <p>Controllers Folder
  -HomeController.cs
  -UsersController.cs</p>
  
  <pre><code>Models
  -Repository Folder
    - UersRepository.cs
  -Repository Interface Folder
    - IUsers.cs
  -Service Folder
    - UsersService.cs
    - IUserService.cs
  -Validation Folder
    - IValidationDictionary
    - ModelStateWrapper
  </code></pre>
  
  <p>The view that I'm trying to partially render is called LoginInfo.ascx and it's located in the Shared Folder.</p>
  
  <p>LoginInfo.cs Code:</p>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;IEnumerable&lt;Users&gt;&gt;" %&gt;
  
  &lt;%@ Import Namespace="TimeAttendanceMVC.Models"%&gt;
  
  
  
  
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;
              UserName
          &lt;/th&gt;
          &lt;th&gt;
              Department
          &lt;/th&gt;
          &lt;th&gt;
              UserType
          &lt;/th&gt;
  
      &lt;/tr&gt;
  
  &lt;% foreach (var item in Model) { %&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;%: Html.DisplayFor(modelItem =&gt; item.UserName) %&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;%: Html.DisplayFor(modelItem =&gt; item.Department) %&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;%: Html.DisplayFor(modelItem =&gt; item.UserType) %&gt;
          &lt;/td&gt;
  
      &lt;/tr&gt;
  &lt;% } %&gt;
  
  &lt;/table&gt;
  </code></pre>
  
  <p>Site.Master Code:</p>
  
  <pre><code>   &lt;% Html.RenderPartial("LoginInfo"); %&gt;
  </code></pre>
  
  <p>UserController.cs</p>
  
  <pre><code> public class UsersController : Controller
      {
          //
          private IUsersService _service;
  
          //==============================================================================
          //==============================================================================
          public UsersController()
          {
            _service = new UsersService(new ModelStateWrapper(this.ModelState));
           }
  
          public UsersController(IUsersService service)
          {
              _service = service; 
          }
  
          //==============================================================================
          //==============================================================================
          // GET: /Employee/
          //==============================================================================
          //==============================================================================
          // GET: /Employee/
          public ActionResult Index()
          {
  
              var model = _service.Return_UserName_Dept();
              return View("LoginInfo", model);
          }
  
      }
  </code></pre>
  
  <p>HomeController.cs</p>
  
  <pre><code> public ActionResult Index()
          {
              //var model = _service.Return_UserName_Dept();
              //return View(model);
              return View();
  
          }
  </code></pre>
  
  <p>With the code above, I get an error.  If I un-comment the 2 lines in my HomeController and I pass the model to the View, then it works fine.  But my HomeController will need a different model, so how will I pass 2 models to the View?  </p>
  
  <p>Any ideas what I'm doing wrong?  I'm still learning MVC right now so I'm not that good at this. Any help would be appreciated.  Thanks.  </p>