title: Model values not carrying into partial view from main view- C# MVC
body: <p>I've been stumped for days.  </p>
  
  <p>I have an index page that contains a renderpartial view. A viewmodel is passed to the index page from its controller, then passed from inside the index.cshtml to the renderpartial view as an extension.  The renderpartial view is automatically updated every 10 seconds (via jquery function to the controller from the index page) to update its content, which works fine.  The index page contains several checkboxfor's that filter out the contents of the renderpartial view.  The problem arises when the initial renderpartial view is called when the time period has elapsed, the controller for the renderpartial view does not have the correct model data the controller for the index had prior. Boolean values in the model which were set to true while in the index controller now are false when we get to the renderpartial view.  Lets begin...</p>
  
  <p>My Index View:</p>
  
  <pre><code>@model SelfServe_Test2.Models.NGTransCertViewModel
  
  ...        
  
      &lt;div class="Services_StatusTable" id="refreshme"&gt;        
      @{            
          Html.RenderPartial("_Data", Model);
      }            
      &lt;/div&gt;
  
  ...
  
  @Html.CheckBoxFor(m =&gt; m.NGTransServicesModel.filter_NJDVSVR24, new { onclick = "test(id)" }) @Html.Label("NJDVSVR24", new { })
  
  ...
  
  &lt;script src="~/Scripts/jquery-1.12.4.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      $(function () {
          setInterval(function () { $('#refreshme').load('/NGTransCertServices/Data'); }, 10000); // every 10 seconds
  
      function test(filter) {
          alert(filter);
          var serviceChecked = document.getElementById(filter).checked;
          $.ajax({
              type: "POST",
              url: "/NGTransCertServices/ToggleVisibleService",
              data: { 'filterOnService': filter, 'serviceChecked': serviceChecked, 'model': @Model },
              //success: function (result) {
              //    if (result === "True")
              //        alert("yup");
              //    else
              //        alert("nope");
              //}
          });
      }
  
  &lt;/script&gt;
  </code></pre>
  
  <p>The PartialView _Data.cshtml:</p>
  
  <pre><code>@model SelfServe_Test2.Models.NGTransCertViewModel
  
  ...
  
  &lt;table&gt;
  foreach (var item in Model.NGTransServicesList)
  {
  
      if (Model.NGTransServicesModel.filter_EBT == true)
      {
          if (item.Description.Contains("EBT"))
          {
  
          }
      }
  }
  &lt;/table&gt;
  </code></pre>
  
  <p>My ViewModel:</p>
  
  <pre><code>namespace SelfServe_Test2.Models
  {
  public class NGTransCertViewModel
  {
      public NGTransCertViewModel()
      {
          NGTransServicesModel = new NGTransCertServicesModel();
          NGTransServicesList = new List&lt;NGTransCertServicesList&gt;();
          NGTransServices = new NGTransCertServices();
      }
      public NGTransCertServicesModel NGTransServicesModel { get; set; }
      public List&lt;NGTransCertServicesList&gt; NGTransServicesList { get; set; }
      public NGTransCertServices NGTransServices { get; set; }
  }
  }
  </code></pre>
  
  <p>The Controller:</p>
  
  <pre><code> public class NGTransCertServicesController : Controller
  {
      NGTransCertViewModel NGT_VM = new NGTransCertViewModel();
      NGTransCertServicesModel certServicesModel = new NGTransCertServicesModel();
  
      public ActionResult Index()
      {            
          NGTransCertServices certServices = new NGTransCertServices();                        
          NGT_VM.NGTransServicesModel = certServices.InitServiceTypeCheckBoxes(certServicesModel);   // sets all checkboxes to true initially.
          return View(NGT_VM);
      }
  
  
      [OutputCache(NoStore = true, Location = System.Web.UI.OutputCacheLocation.Client, Duration = 10)]    // in seconds
      public ActionResult Data()
      {
          NGTransCertDBHandle certDBHandle = new NGTransCertDBHandle();
          List&lt;NGTransCertServicesList&gt; List_certServices = certDBHandle.GetService();
          return PartialView("_Data", NGT_VM);
      }
  }
  </code></pre>
  
  <p>Finally, the model where the values are lost:</p>
  
  <pre><code> public class NGTransCertServicesModel
  {
      ...
      public bool filter_NJDVSVR24 { get; set; }
      ...
  }
  </code></pre>
  
  <p>Now then, when the Index.cshtml page is called, i run the InitServiceTypeCheckBoxes method that sets the checkbox values to true, pass the viewmodel to the index page and pass that same model to the renderpartial. All is happy until the 10s timeout is reached and _Data.cshtml is rendered.  The checkbox values are now all false.</p>
  
  <p>Let me add a visual element. Below is the model when returning from the controller to the index view with the Boolean set to true as desired. (stepping through)
  <a href="https://i.stack.imgur.com/7XErD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7XErD.png" alt="enter image description here"></a></p>
  
  <p>Below is the model when the index view
  <a href="https://i.stack.imgur.com/OOqHf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OOqHf.png" alt="enter image description here"></a></p>
  
  <p>Again, in the _Data.cshtml partial view
  <a href="https://i.stack.imgur.com/RV4kH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RV4kH.png" alt="enter image description here"></a></p>
  
  <p>Now with a breakpoint in the Data action in the controller, that same bool value is now false
  <a href="https://i.stack.imgur.com/t1DtP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t1DtP.png" alt="enter image description here"></a></p>
  
  <p>The bool does not have the true value even before the first line of code in the Data action.</p>
  
  <pre><code>NGTransCertDBHandle certDBHandle = new NGTransCertDBHandle();
  </code></pre>