title: Display Message To View
body: <p>I want to display a message to my view from my controller but its doesn't happen anything. I am using ViewBag.Message to display the message. I dont know if i'm making a mistake in my code or this is not properly way to do it. This is my code:</p>
  
  <p>Controller:</p>
  
  <pre><code>[HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create([Bind(Include = "Id_IngresoM,Id_Componente,Lote,Serie,Cantidad,Id_Usuario")] IngresoMateriales ingresoMateriales)
      {
          var user = "1ef69472-1b7d-460d-a6f9-9d458c5e314e";
          string msj = "";
          try
          {
              var affectedRows = db.Database.ExecuteSqlCommand("IngresoMaterialesInspeccion @IdComponente, @Lote, @Serie, @Cantidad, @IdUsuario",
                                           new SqlParameter("@IdComponente", ingresoMateriales.Id_Componente),
                                           new SqlParameter("@Lote", ingresoMateriales.Lote),
                                           new SqlParameter("@Serie", ingresoMateriales.Serie),
                                           new SqlParameter("@Cantidad", ingresoMateriales.Cantidad),
                                           new SqlParameter("@IdUsuario", user));
  
              //ModelState.AddModelError("", "El Certificado no esta Vigente");
              if (affectedRows == '3')
              {
                  msj = "El Certificado no esta Vigente";
              }
              else if (affectedRows == '2')
              {
                  msj = "El Componente esta libre de Inspeccion";
              }
              else if (affectedRows == '1')
              {
                  msj = "Componente Sospechoso";
              }
              else
              {
                  msj = "Pues nada";
              }
              ViewBag.Message = msj;
              return RedirectToAction("Create");
          }
          catch (SqlException ex)
          {
              foreach (SqlError Error in ex.Errors)
              {
                  return new JavaScriptResult { Script = Error.ToString() };
              }
          }
  
          ViewBag.Message = msj;   ------------------------------
          return View();  
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.Serie, htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              &lt;input class="form-control" type="text" id="Serie" name="Serie" onkeypress="FunctionS(event)" /&gt;
              @Html.ValidationMessageFor(model =&gt; model.Serie, "", new { @class = "text-danger" })
          &lt;/div&gt;
      &lt;/div&gt;
  
  
      @if (ViewBag.Message != null)
      {
          &lt;script type="text/javascript"&gt;
          window.onload = function () {
              alert("@ViewBag.Message");   ---------------------
          };
          &lt;/script&gt;
      }
  &lt;/div&gt;
  </code></pre>