Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying modelstate error

I am having the following code, However the errors causght not being displayed. What is wrong ?

  public ActionResult DeleteRateGroup(int id)
    {
        try
        {
           RateGroup.Load(id).Delete();

            RateGroupListModel list = new RateGroupListModel();
            return GetIndexView(list);
        }
        catch (Exception e)
        {
            RateGroupListModel model = new RateGroupListModel(); 

            if (e.InnerException != null)
            {
                if (e.InnerException.Message.Contains("REFERENCE constraint"))
                    ModelState.AddModelError("Error", "The user has related information and cannot be deleted.");
            }
            else
            {
                ModelState.AddModelError("Error", e.Message);
            }
            return RedirectToAction("RateGroup", model);
        }
    }

    @model MvcUI.Models.RateGroupListModel

@{
    View.Title = "RateGroup";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Rate Group</h2>

@Html.ValidationSummary()

@using (Html.BeginForm())

    private ActionResult GetIndexView(RateGroupListModel model)
    {
       return View("RateGroup", model);
    }

    public ActionResult RateGroup(RateGroupListModel model)
    {
       return GetIndexView(model);
    }
like image 399
learning Avatar asked Jan 16 '11 13:01

learning


People also ask

How do I display ModelState errors?

The ModelState object is a dictionary and it has a keys property that will contain the name of the property in the model causing the error. The keys property is a collection of all the keys in the dictionary. You can get the name of the first key by doing something like this: var firstPropertyName = ModelState.

How do I get ModelState errors in my controller?

You can use SelectMany function c# to get error message from modelstate mvc. It will generate error message string it contains modelstate errors; we can return as json and display in html element. You have to define validations inside the class as per requirement.

How do I know if my ModelState is valid?

Below the Form, the ModelState. IsValid property is checked and if the Model is valid, then the value if the ViewBag object is displayed using Razor syntax in ASP.Net MVC.

Which property is used to determine an error in ModelState?

Errors property and the ModelState. IsValid property. They're used for the second function of ModelState : to store the errors found in the submitted values.


1 Answers

It looks like you're setting the ModelState error, then redirecting to another action. I'm pretty sure the ModelState gets lost when you do that.

Typically, you'd just render the RateGroup view directly from the DeleteRateGroup action, without the redirect, passing in your model if needed, like this:

return View("RateGroup", model);

If you want the ModelState to come along to the second action with you, take a look at MvcContrib's ModelStateToTempDataAttribute. Here's the attribute's description, from the MvcContrib source code's comments:

When a RedirectToRouteResult is returned from an action, anything in the ViewData.ModelState dictionary will be copied into TempData. When a ViewResultBase is returned from an action, any ModelState entries that were previously copied to TempData will be copied back to the ModelState dictionary.

like image 162
James Nail Avatar answered Sep 21 '22 11:09

James Nail