title: Using TempData inside scripts when returning a PartialView
body: <p>I am trying to pass TempData value to my view, but my controller action is an ajax post controller and returns a partialview.</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Create(CategoryViewModel model)
  {
      if (ModelState.IsValid)
      {
          var category = new Category()
          {
              Name = model.Name,
              Description = model.Description
          };
          //test
          System.Threading.Thread.Sleep(2000);
  
          try
          {
              _repository.AddCategory(category);
  
              TempData["success"] = string.Format("Category Added");
              return PartialView("~/Areas/Dashboard/Views/Category/_List.cshtml", CategoryListMap());
          }
          catch(Exception ex)
          {
              TempData["error"] = string.Format("{0}", ex);
              return PartialView("~/Areas/Dashboard/Views/Category/_List.cshtml", CategoryListMap());
          }
      }
  
      TempData["error"] = string.Format("Modal state is not valid");
      return PartialView("~/Areas/Dashboard/Views/Category/_List.cshtml", CategoryListMap());
  }
  </code></pre>
  
  <p>My partial view of a create form:</p>
  
  <pre><code>@using (Ajax.BeginForm("Create", "Category", new { area = "dashboard" }, new AjaxOptions { UpdateTargetId = "categories", OnComplete = "onComplete", OnBegin = "onBegin" }))
  {
      @Html.AntiForgeryToken()
      &lt;div class="modal-body"&gt;
          &lt;div class="form-group"&gt;
              @Html.LabelFor(m =&gt; m.Name)
              @Html.TextBoxFor(m =&gt; m.Name, new { @class = "form-control" })
              @Html.ValidationMessageFor(m =&gt; m.Name)
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              @Html.LabelFor(m =&gt; m.Description)
              @Html.TextAreaFor(m =&gt; m.Description, new { @class = "form-control", rows = "5" })
              @Html.ValidationMessageFor(m =&gt; m.Description)
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="modal-footer"&gt;
          &lt;button type="button" class="btn btn-white" data-dismiss="modal"&gt;Close&lt;/button&gt;
          &lt;button class="ladda-button btn btn-primary" type="submit" data-style="zoom-in"&gt;Add&lt;/button&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>So my controller must returns a partialview because of the ajax form, and the value TempData is passed to the _list <strong>partialview</strong>.</p>
  
  <pre><code>@if (TempData["error"] != null)
  {
      &lt;p&gt;
          &lt;div class="alert alert-danger alert-dismissable"&gt;
              &lt;button aria-hidden="true" data-dismiss="alert" class="close" type="button"&gt;×&lt;/button&gt;
              @TempData["error"]
          &lt;/div&gt;
      &lt;/p&gt;
  }
  @if (TempData["success"] != null)
  {
      &lt;p&gt;
          &lt;div class="alert alert-success alert-dismissable"&gt;
              &lt;button aria-hidden="true" data-dismiss="alert" class="close" type="button"&gt;×&lt;/button&gt;
              @TempData["success"]
          &lt;/div&gt;
      &lt;/p&gt;
  }
  
  &lt;table class="table table-striped table-bordered table-hover dataTables"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;#&lt;/th&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          @foreach (var category in Model)
          {
              &lt;tr&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(m =&gt; category.CategoryId)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(m =&gt; category.Name)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(m =&gt; category.Description)
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
      &lt;/tbody&gt;
      &lt;tfoot&gt;
          &lt;tr&gt;
              &lt;th&gt;#&lt;/th&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/tfoot&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>As you can see I'm getting the TempData value inside my partialview, everything is working fine but <strong>the problem I'm having is how do I get the TempData value inisde my view instead?</strong> I tried to get the TempData value inside the view itself but its not working because I think in my controller I'm returning a partialview and the TempData value will only display when a partialview is been requested?</p>
  
  <p>Here is my full view:</p>
  
  <pre><code>&lt;div class="row wrapper border-bottom white-bg page-heading"&gt;
      &lt;div class="col-lg-10"&gt;
          &lt;h2&gt;Categories&lt;/h2&gt;
          &lt;ol class="breadcrumb"&gt;
              &lt;li&gt;
                  &lt;a href="@Url.Action("Index", "Category")"&gt;Categories&lt;/a&gt;
              &lt;/li&gt;
              &lt;li class="active"&gt;
                  &lt;strong&gt;List&lt;/strong&gt;
              &lt;/li&gt;
          &lt;/ol&gt;
      &lt;/div&gt;
      &lt;div class="col-lg-2"&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="wrapper wrapper-content animated fadeInRight"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-lg-12"&gt;
              &lt;div class="ibox float-e-margins"&gt;
                  &lt;div class="ibox-title"&gt;
                      &lt;button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal1"&gt;
                          &lt;i class="fa fa-plus"&gt;&lt;/i&gt;
                      &lt;/button&gt;
                  &lt;/div&gt;
  
                  &lt;div class="ibox-content table-responsive"&gt;
                      &lt;div id="categories"&gt;
                          @{Html.RenderAction("List", "Category", new { area = "dashboard" });}
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="modal inmodal" id="myModal1" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static"&gt;
      &lt;div class="modal-dialog"&gt;
          &lt;div class="modal-content animated fadeIn"&gt;
              &lt;div class="modal-header"&gt;
                  &lt;button type="button" class="close" data-dismiss="modal"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;span class="sr-only"&gt;Close&lt;/span&gt;&lt;/button&gt;
                  &lt;h4 class="modal-title"&gt;Add a category&lt;/h4&gt;
              &lt;/div&gt;
              @{ Html.RenderAction("Create", "Category", new { area = "dashboard" });}
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  @section Scripts {
      @Scripts.Render...blahblah
  
      &lt;script type="text/javascript"&gt;
  
          function onSuccess() {
              //remove toastr
              toastr.remove();
              **//not working, tempdata value not set**
              toastr.success('@TempData["success"]', 'Success');
          }
  
          function onFailure() {
              //remove toastr
              toastr.remove();
              **//not working, tempdata value not set**
              toastr.error('@TempData["error"]', 'Error');
          }
      &lt;/script&gt;
  }
  </code></pre>
  
  <p>If I try to use TempData value inside the full view, no value is set...I can't render Scripts inside a partialview and from my understanding you shouldn't write any scripts inside a partialview as well, <strong>so how can I use the tempdata value inside the script when I'm returning a partial view with tempdata???</strong></p>