ViewData ["ErrorMessage"] does not appear in view

3

Hello, I'm new to the C # language and would like to know why my ViewData ["ErrorMessage"] does not appear in my view. Controller:

ViewData["ErrorMessage"] = "O e-mail digitado já se encontra registrado em nossa base de dados.";
return RedirectToAction("Index","Home");

View:

 <form asp-controller="Account" asp-action="Visitante" method="post" class="form-horizontal" role="form">
            @if (ViewData["ErrorMessage"] != null)
            {
                @ViewData["ErrorMessage"];
                <br />
            } 
            <div asp-validation-summary="ValidationSummary.All" class="text-danger"></div>
            <div class="form-group">
                <div class="col-md-6">
                    <input asp-for="Nome" class="form-control" placeholder="Nome" />
                    <span asp-validation-for="Nome" class="text-danger"></span>
                </div>
                <div class="col-md-6">
                    <input asp-for="Sobrenome" class="form-control" placeholder="Sobrenome" />
                    <span asp-validation-for="Sobrenome" class="text-danger"></span>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-12">
                    <input asp-for="Email" class="form-control" placeholder="E-Mail" />
                    <span asp-validation-for="Email" class="text-danger"></span>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-12">
                    <input type="submit" class="btn btn-default" style="width: 100%" value="Cadastre-se agora!" data-toggle="tooltip" data-placement="bottom" data-original-title="Cadastre-se" />
                </div>
            </div>
        </form>

See that in my view I make a @if to check if my ViewData is null, if not I want it to display the message, but from what I see ViewData does not contain the phrase I assigned no controller.

Thank you.

    
asked by anonymous 11.09.2015 / 19:29

1 answer

3

Since you are using RedirectToAction , ViewData is lost, so you should use TempData .

The TempData uses Session to save the data, it gets lost after the display.

The lifetime of ViewData is only between sending through the Controller and viewing in the View.

See more in this article = link

    
11.09.2015 / 19:32