But I read somewhere that I should put one property in my view model
and set the error message on that property. I am confused now, how to
achieve that and how to display the error in View then? And which one
is the recommended/best practice?
The best practice is to alter the ModelState
dictionary property of your controller like this:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Search(ForgotPasswordMV viewModel)
{
// ...
else
{
ModelState.AddModelError(nameof(ForgotPasswordMV.Email), "Email not found or matched");
return View(viewModel);
}
}
Then in your view add the line below next to your email field;
@Html.ValidationMessageFor(m => m.Email)