Access time c# and show in Alert

Asked

Viewed 37 times

1

I am trying to display a message according to the return of my method, which is a Task<ActionResult> , but when I call the TempData, she’s coming empty, where I’m erring?

@section scripts {
  <script type="text/javascript">
  $("#button-submit").click(function ()
    {
      alert('@(TempData["CurrentTab"])');
    })
  </script>
}

these are the returns of my form

    [HttpPost]
    public async Task<ActionResult> Index(ApiLServico.ContatoObject _user, string cpf,
     string ddd, string telefone, int Niveis)
         TempData["Message"] = res;

             {...condições...}

          return View();
        }
        TempData["Message"] = "Ocorreu um erro";
        return RedirectToAction("Index", "Home");
      }
      catch (Exception ex) { TempData["Message"] = ex.ToString(); 
    return RedirectToAction("Index", "Home"); } 
    }
    TempData["Message"] = "Por favor preencha todos os campos" ;
      return RedirectToAction("Index", "Home");

How can I make it work properly by passing the messages I want?

1 answer

1


I got a solution, actually I just needed to assign the variable

 @section scripts {
   <script type="text/javascript">
     $("#button-submit").click(function () {

       var message = '@TempData["Message"]';
         alert(message);
     })
   </script>
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.