Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add ModelState.AddModelError message when model item is not binded

Tags:

I am new to MVC4. Here I added the ModelState.AddModelError message to display when the delete operation is not possible.

  <td>     <a id="aaa" href="@Url.Action("Delete", "Shopping", new { id = Request.QueryString["UserID"], productid = item.ProductID })" style="text-decoration:none">     <img alt="removeitem" style="vertical-align: middle;" height="17px" src="~/Images/remove.png"  title="remove" id="imgRemove" />       </a>       @Html.ValidationMessage("CustomError")     </td>      @Html.ValidationSummary(true) 


In my controller

public ActionResult Delete(string id, string productid)         {                          int records = DeleteItem(id,productid);             if (records > 0)             {               ModelState.AddModelError("CustomError", "The item is removed from your cart");                return RedirectToAction("Index1", "Shopping");             }             else             {                 ModelState.AddModelError(string.Empty,"The item cannot be removed");                 return View("Index1");             }         } 

Here I didnt pass any of the model item in the View to check for the item in Model and I couldnt get the ModelState error message ..
Any suggestions

like image 473
kk1076 Avatar asked Oct 17 '12 14:10

kk1076


People also ask

How do I add an error message in ModelState?

Above, we added a custom error message using the ModelState. AddModelError() method. The ValidationSummary() method will automatically display all the error messages added into the ModelState .

What does ModelState AddModelError do?

AddModelError(String, String)Adds the specified error message to the errors collection for the model-state dictionary that is associated with the specified key.

How do you add model error in the controller?

How to handle error in controller action method and pass error to the View? 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.

What does ModelState IsValid validate?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process.


2 Answers

The ModelState is created at each request so you should use TempData.

public ActionResult Delete(string id, string productid) {                  int records = DeleteItem(id,productid);     if (records > 0)     {             // since you are redirecting store the error message in TempData         TempData["CustomError"] = "The item is removed from your cart";         return RedirectToAction("Index1", "Shopping");     }     else     {         ModelState.AddModelError(string.Empty,"The item cannot be removed");         return View("Index1");     } }  public ActionResult Index1() {     // check if TempData contains some error message and if yes add to the model state.     if(TempData["CustomError"] != null)     {         ModelState.AddModelError(string.Empty, TempData["CustomError"].ToString());     }      return View(); } 
like image 88
VJAI Avatar answered Sep 18 '22 06:09

VJAI


RedirectToAction will clear ModelState. You must return a view in order to use this data. Therefore, the first if case won't work. Also, ensure that you have a control in your view (like ValidationSummary) which displays the error... this could be the problem in the second case.

like image 31
Levi Botelho Avatar answered Sep 19 '22 06:09

Levi Botelho