title: How to get the value of a ViewBag with Jquery
body: <p>I am passing a ViewBag with "error" value when the user does not complete the form, and with jquery I am placing the ViewBag in an alert so that the user can see the message. But I do not get any results.</p>
  
  <p>Also, I understand that the GET and POST in .net are 2 different pages, the GET shows the View and the POST returns to show the View but with the validated data. In my opinion, I see that actually only one page is displayed, since I have put an alert () to determine if it is true and when the page loads the message appears, but when I press the submit button, the message does not appear again.</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model wsCharlas.Models.ClsPrueba
  
  @{
      ViewBag.Title = "Prueba";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  &lt;h2&gt;Prueba&lt;/h2&gt;
  
  
  @using (Html.BeginForm()) 
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;ClsPrueba&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.id_prueba, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.id_prueba, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.id_prueba, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.nombre_prueba, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.nombre_prueba, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.nombre_prueba, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  
  &lt;script type="text/javascript"&gt;
  
      $(document).ready(function () {
  
         alert("Hello World");        
  
         if ('@ViewBag.msg' != "") {
              alert('@ViewBag.msg');
         }
      });
  
  
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>    public ActionResult Prueba()
      {
          return View(new ClsPrueba());
      }
  
      [HttpPost]
      public ActionResult Prueba(ClsPrueba prueba)
      {
          if (!ModelState.IsValid)
          {
              ViewBag.msg = "error";
              return View(prueba);
          }
  
          return View(prueba);
      }
  </code></pre>