title: ASP.Net MVC: Passing view model to partial view
body: <p>I've read a bunch of posts here on SO re: this and still don't get it.</p>
  
  <p>I have a view with a partial view paging control in it that needs access to the same model as the parent view.</p>
  
  <p>Parent View:</p>
  
  <pre><code>model IEnumerable&lt;Models.ExchangeBrowseViewModel&gt;
  @{
      ViewBag.Title = metaExchange.Index_PageTitle;
  }
  @section styles
  {        
      &lt;link rel="stylesheet" type="text/css" href="/incl/css/bp-default.css" /&gt;
      &lt;link rel="stylesheet" type="text/css" href="/incl/css/bp-component.css" /&gt; 
      &lt;link rel="stylesheet" type="text/css" href="/incl/css/paging.css" /&gt;   
  }
  &lt;hgroup&gt;
      &lt;h2&gt;@metaExchange.Index_InlineTitle&lt;/h2&gt;
      &lt;h3&gt;@ViewBag.Message&lt;/h3&gt;
  &lt;/hgroup&gt;
  
  &lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-2" style="border: 1px solid #000000"&gt;
               @Html.Partial("_ItemsSearch_BasicPartial");         
          &lt;/div&gt;
          &lt;div class="col-md-10"&gt;
              @Html.Action("Pagination", new {showWell=false, model=Model});
  </code></pre>
  
  <p>Pagination Action:</p>
  
  <pre><code>[AllowAnonymous]
      [ChildActionOnly]
      public ActionResult Pagination(ExchangeBrowseViewModel model, bool showWell)
      {
          ViewBag.ShowWell = showWell;
          return PartialView("_ItemsList_Pagination", model);
      }
  </code></pre>
  
  <p>Pagination Partial View</p>
  
  <pre><code>@model IPagedList&lt;Models.ExchangeBrowseViewModel&gt;
  &lt;div class="mvcpagination"&gt;
      @Html.Raw(Ajax.Pager(
          new Options
              {
                  PageSize = Model.PageSize,
                  TotalItemCount = Model.TotalItemCount,
                  CurrentPage = Model.PageNumber,
                  ItemTexts = new ItemTexts() {Next = "&gt;&gt;", Previous = "&lt;&lt;", Page = ""},
                  ItemIcon = new ItemIcon() {First = "icon-backward", Previous = "icon-chevron-left", Next = "icon-chevron-right", Last = "icon-forward"},
                  TooltipTitles = new TooltipTitles() {Next = "Next", Previous = "Previous", Page = "Page {0}."},
                  Size = Size.normal,
                  Alignment = Alignment.right,
                  IsShowControls = true,
                  IsShowFirstLast = true,
                  CssClass = "light-theme"
              },
          new AjaxOptions
              {
                  UpdateTargetId = "grid-list",
                  OnBegin = "beginPaging",
                  OnSuccess = "successPaging",
                  OnFailure = "failurePaging"
              }, new {controller = "Requests", action = "Index", requestTitle = ViewData["requestTitle"]}))
      @if (ViewBag.ShowWell)
      {
          &lt;div class="well"&gt;
              Showing &lt;span class="badge badge-success"&gt;@Model.ItemStart&lt;/span&gt; to &lt;span class="badge badge-success"&gt;@Model.ItemEnd&lt;/span&gt;
              of &lt;span class="badge badge-info"&gt;@Model.TotalItemCount&lt;/span&gt; entries&lt;/div&gt;
      }
  &lt;/div&gt;
  </code></pre>
  
  <p>The problem is the model is always null inside the action method and therefore the partial throw a NullReference exception trying to access model properties.</p>