Viewdata["Errormessage"] does not appear in view

Asked

Viewed 184 times

3

Hi, I’m new to c# 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 one @if to check if my ViewData is null, if not want to display the message, further by what I see the ViewData does not contain the phrase I designated in the controller.

Thank you.

1 answer

3


How are you utilizing the RedirectToAction the ViewData if lost, for this you must use the TempData.

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

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

See more in this article = http://eduardopires.net.br/2013/06/asp-net-mvc-viewdata-viewbag-tempdata/

  • 1

    In addition, this link explains the differences between viewData, tempData :)) http://www.macoratti.net/15/06/mvc_conc1.htm

  • 1

    Thanks, very good article, I’ll take a look

Browser other questions tagged

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