title: ASP.NET MVC Use javascript value in partial view
body: <p>I have a view with some javascript in it. The javascript has a callback function that is called, when the data i need is ready (from indexeddb).</p>
  
  <p>This data, i want to pass to a partial view - How do i do that? I cannot seem to use ViewData, as that expires as soon as the page renders (thus, before the callback is received). Any ideas? Code is here:</p>
  
  <pre><code>&lt;div&gt;
      &lt;script type="text/javascript"&gt;
          var somecallback = myFunctionDefinedSomewhere();
          somecallback.onsuccess = function(evt) {
              console.log("Success!");
  
              var iReallyNeedThisVariable = evt.id;
              @ViewData["iReallyNeedThisVariable"] = iReallyNeedThisVariable;
          }
      &lt;/script&gt;
  
      @for (int i = 0; i &lt; Model.MyCollection.Count; i++)
      {
          if (i == 0)
          {
              @Html.Partial("_MyPartialView", Model.MyCollection.ElementAt(i), new ViewDataDictionary { { "Stuff", true }, { "iReallyNeedThisVariable", @ViewData["iReallyNeedThisVariable"] } });
          }
          else
          {
              @Html.Partial("_MyPartialView", Model.MyCollection.ElementAt(i), new ViewDataDictionary { { "Stuff", false }, { "iReallyNeedThisVariable", @ViewData["iReallyNeedThisVariable"] } });
          }
      }
  &lt;/div&gt;
  </code></pre>