Display error message in view from asp.net mvc 5 controller

I am new to web development and trying to learn ASP.Net MVC 5. I am looking for one entry in the database, if the entry is not found, then I want to display an error message to the user. Below is my attempt:

controller

[HttpGet] public ActionResult Search() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Search(ForgotPasswordMV viewModel) { if (Temp.Check(viewModel.Email)) return RedirectToAction("VerifyToken", new { query = viewModel.Email }); else { ViewBag.ErrorMessage = "Email not found or matched"; return View(); } } 

View:

 <p>@ViewBag.ErrorMessage</p> 

ViewModel

 public class ForgotPasswordMV { [Display(Name = "Enter your email"), Required] public string Email { get; set; } } 

But I read somewhere that I have to put one property in my view model and set an error message for this property. Now I am confused how to do this and how to display the error in the view? And which of the recommended / best practices?

+23
c # asp.net-mvc asp.net-mvc-5
source share
4 answers

But I read somewhere that I have to put one property in my model and set this error message. I am confused now, how is it and how to display the error in the view? And what is the recommended / best practice?

Best practice is to modify your controller's ModelState dictionary ModelState as follows:

 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Search(ForgotPasswordMV viewModel) { // ... else { ModelState.AddModelError("Email", "Email not found or matched"); return View(viewModel); } } 

Then in your view add a line next to the email field;

 @Html.ValidationMessageFor(m => m.Email) 
+37
source share

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.

+3
source share

For me, adopting anwser is not a good practice. We can handle all errors in annotations.
In our ViewModel, we specify ErrorMessage for our purposes.

 public class UserLoginViewModel { [Required(ErrorMessage = "User name is required")] [Display(Name = "User name")] [StringLength(500, ErrorMessage = "User name is too short", MinimumLength = 3)] public string Login { get; set; } [Required(ErrorMessage = "Password is required")] [Display(Name = "Password")] public string Password { get; set; } } 

In our controller

 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Login(UserLoginViewModel model) { if (!this.ModelState.IsValid) { return this.View(); } ... } 

And our look

 @Html.ValidationMessageFor(model => model.Login) @Html.EditorFor(model => model.Login) @Html.ValidationMessageFor(model => model.Password) @Html.EditorFor(model => model.Password) 
+1
source share

If someone is looking for a simple fix and NOTHING CONSTANT, feel free to use this answer as it helped me. DO NOT use this hotfix if you need to worry about the security of your application.

In your controller:

 TempData["Message"] = "This is my Error"; 

In your Error.cshtml file:

 <h3><strong>@TempData["Message"]</strong></h3> 

Result:

enter image description here

0
source share

All Articles