But I read somewhere that I had to put one property in my view model and set an error message in this property.
It is right. You can add an error message to your view model:
public class ForgotPasswordMV { [Display(Name = "Enter your email"), Required] public string Email { get; set; } public string ErrorMessage { get; set; } }
and then set this property in your view model and pass the view model to the view:
... else { viewModel.ErrorMessage = "Email not found or matched"; return View(viewModel); }
and finally, in your strongly typed view, use the property on your model:
@model ForgotPasswordMV ... <p>@Model.ErrorMessage</p>
So basically here we are replacing the use of ViewBag with a strongly typed view model.
Darin Dimitrov
source share