How to display a message in the view via a viewdata

Asked

Viewed 150 times

0

How to create a message via a viewdata to the view?

In the code below I am trying to validate if my user is null, it has to display the error on the screen for the user.

Class

public async Task<IActionResult> OnPostAsync()
    {

        try
        {


            var user = await _userManager.FindByEmailAsync(Input.Email);

            if (user == null)
            {
                ViewData["mensagem"] = "teste";
                return RedirectToPage();
            }
            else
            {
                var code = await _userManager.GeneratePasswordResetTokenAsync(user);
                var callbackUrl = Url.Page(
                    "/Account/ResetPassword",
                    pageHandler: null,
                    values: new { code },
                    protocol: Request.Scheme);

                Email email = new Email(_email);
                await email.AlterarSenha(user.UserName, callbackUrl, _env.WebRootPath);

                return RedirectToPage("./ForgotPasswordConfirmation");
            }





        }
        catch (Exception)
        {

            throw;
        }
    }

View:

 <section class="section">
    <div class="container">
        <h2>Esqueci minha senha</h2>
        <span>Para redefinir sua senha, basta inserir o e-mail no campo abaixo e clicar no botão </span><strong>Recuperar Senha</strong>
        @ViewData["mensagem"]
        <div class="row">
            <div class="col-md-4">
                <form method="post">
                    <div asp-validation-summary="All" class="text-danger"></div>
                    <div class="form-group">
                        <input asp-for="Input.Email" class="form-control" />
                        <span asp-validation-for="Input.Email" class="text-danger"></span>
                    </div>
                    <button type="submit" class="btn btn-primary btn-sm">Recuperar Senha</button>
                </form>
            </div>
        </div>
    </div>
</section>
  • I don’t quite understand what you want, but in View just use anywhere you want to access the message <%= ViewData["mensagem"]%>, which is exactly the problem?

  • It is that I am trying to pass a message to view through a viewdata, but when it falls in my condition and fills the viewdata where I have it rendered it does not display the message

  • For example: If my condition is null in the controllers it fills this viewdata and shows on the screen to the user, only I’m not able to make the message display on the screen to the user.

  • Ready I answered your doubt see if you understood?

1 answer

0


Analyzing your code below:

if (user == null)
{
    ViewData["mensagem"] = "teste";
    return RedirectToPage();
}

is done in code one ViewData["messagem"] and then a redirect to the method OnGet from that same page, then, automatically ViewData will have no value, because of the redirect.

How should I solve this problem, example:

public void OnGet()
{
    if (TempData.TryGetValue("messagem", out object value))
    {
        ViewData["messagem"] = value;
    }
}

public IActionResult OnPost()
{
    TempData["messagem"] = "Enviando mensagem ...";
    return RedirectToPage();
}

Explaining: the first method with the name OnGet server to open your page and method OnPost server to redeem submitted information from a form (<form/>), I think this is known. Note in the method code OnPost was used the TempData which has the function of passing information to the other request, where the method OnGet asks if there is any information on TempData so that the information is really brought to this new request. Your mistake was at this point where you created the information, but it was lost in the request by the command RedirectToPage().

Finalizing: to transmit information in request methods use the TempData which is a temporary data rescued on your first request and ViewData used for the request itself to pass information to View.

Browser other questions tagged

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