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?– Ricardo Pontual
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
– Thiago Correa
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.
– Thiago Correa
Ready I answered your doubt see if you understood?
– novic