Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display error message on the view from controller asp.net mvc 5

I am new to web development and trying to learn ASP.Net MVC 5. I am looking for one record in database if the record 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 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?

like image 565
Unbreakable Avatar asked Apr 22 '17 15:04

Unbreakable


People also ask

How can send error message from Controller view in ASP NET MVC?

To pass error to the view we can use ModelState. AddModelError method (if the error is Model field specific) or simply ViewBag or ViewData can also be used.


2 Answers

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) 
like image 187
CodeNotFound Avatar answered Sep 20 '22 15:09

CodeNotFound


But I read somewhere that I should put one property in my view model and set the error message on that property.

That's correct. You could add the 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 on 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.

like image 44
Darin Dimitrov Avatar answered Sep 17 '22 15:09

Darin Dimitrov