title: Toast notifications in ASP.NET MVC
body: <p>I'm using the Toastr notification plugin in my MVC application to display status messages (successful edit, update, delete, etc), and I'm wondering if there is an easy way to put some logic in a Partial view and have it on my Layout or in each individual view where needed.</p>
  
  <h2>Partial</h2>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function () {
          @if (ViewBag.Success == true) { 
              @:toastr.success("@ViewBag.Message");
          } else if (ViewBag.Success == false) {
              @:toastr.error("@ViewBag.Message");      
          }
      });
  &lt;/script&gt;
  </code></pre>
  
  <h2>View</h2>
  
  <pre><code>//Doesn't work
  @Html.Partial("_ToastPartial")
  
  //Tried this directly in the view instead of using the partial, didn't work
  @if (ViewBag.Success == true) { 
      @:toastr.success("@ViewBag.Message");
  } else if (ViewBag.Success == false) {
      @:toastr.error("@ViewBag.Message");  
  }
  </code></pre>
  
  <h2>Controller</h2>
  
  <pre><code>    public ActionResult SomethingAwesome(MyViewModel model)
      {
          ViewBag.Success = true;
          ViewBag.Message = "Employee successfully added";
  
          return RedirectToAction("Index");
      }
  </code></pre>
  
  <p>This doesn't work. Is it possible to wrap something like this in a partial or does MVC strip out the <code>&lt;script&gt;</code> tags? Can I render the partial within a script block on the View?</p>
  
  <p>I even tried to move the code within the script tags directly to the View and then setting the values on the Controller and it seems like nothing is happening.</p>
  
  <p>Any help? Is the ViewBag cleared by the time the view is rendered again? Should I use TempData? Can I move a <code>Success</code> and <code>Message</code> property into my ViewModel and just pass that into my View like this?</p>
  
  <pre><code>public ActionResult Index(MyViewModel model) 
  {
      //Do something with model.Success 
  }
  </code></pre>
  
  <h2>My Solution</h2>
  
  <p>I used a couple of answers to come to a final conclusion for this section of my site, and will most likely use the method for the accepted answer in other parts.</p>
  
  <p>I went and added the following to my ViewModel </p>
  
  <pre><code>public bool Success { get; set; } 
  public string Message { get; set; } 
  </code></pre>
  
  <p>I have the Action return the Index view with the ViewModel, after properties have all been set </p>
  
  <pre><code>//fetch the updated data and shove into ViewModel here
  viewModel.Success = true;
  viewModel.Message = "Employee successfully checked in";
  
  return View("Index", viewModel);
  </code></pre>
  
  <p>And then just use the Razor syntax in my View</p>
  
  <pre><code>@if (Model.Success) { 
      @:toastr.success("@Model.Message");
  } else if (!Model.Success &amp;&amp; !String.IsNullOrWhiteSpace(Model.Message)) {
      @:toastr.error("@Model.Message");   
  }
  </code></pre>
  
  <p>Also as a bonus (although implementation seems sloppy), render the Partial in my document.Ready block </p>
  
  <pre><code>@Html.Partial("_ToastPartial", Model)
  </code></pre>
  
  <p>and move the Toastr notification code to the partial</p>
  
  <pre><code>@if (Model.Success) { 
      @:toastr.success("@Model.Message");
  } else if (!Model.Success &amp;&amp; !String.IsNullOrWhiteSpace(Model.Message)) {
      @:toastr.error("@Model.Message");   
  }
  </code></pre>
  
  <p>Most likely I would set up an interface with the <code>Message</code> and <code>Success</code> properties and make any ViewModels that will use the partial implement that interface</p>