ASP.NET MVC > Error handling

Passing error to view from controller action in ASP.NET MVC

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.

Passing error using ModelState.AddModelError method

First, let's see how to pass error message to the view using ModelState.AddModelError method.

CONTROLLER CODE

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult CreateForValidation(PersonalDetail model)
{
    if (ModelState.IsValid)
    {
        if (model.FirstName == null)
        {
            ModelState.AddModelError("", "Please write first name.");
          // ModelState.AddModelError("FirstName", "Please write first
          name.");
        }
    }

    return View(model);
}

View code

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <fieldset>
        <legend>PersonalDetail</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

Above action method accepts PersonalDetail model whose FirstName is mandatory (We are assuming that FirstName field is not specified as Required in the PersonalDetail.cs file, read more about model and implementating validation). If we want to validate this property in the action method and pass error to the View, we will use ModelState.AddModelError method.

ModelState.AddModelError method accepts two parameters

  • Key – First parameter, if empty shows error written in the 2nd parameter at place where                                                                        
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    is written in the view. If key is specified as “FieldName” (in this case “FirstName” – see the commented code above) then the error  message appears where                                                   
    @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
    
    is written (generally beside the FirstName text box).                                                                                                                                                                                                                                                                                                          

    Generally @Html.ValidationSummary and @Html.ValidationMessageFor is written automatically when we scaffold view from the Model at the time of creating the controller. See any Create/Edit view created with scaffolding.

                                                                                                                                  
  • ErrorMessage – Second parameter is error message to pass to the view            

Passing data using ViewBag or ViewData            

In this case, simply set the value of VIewBag and write on the view wherever required.

CONTROLLER CODE 

[HttpPost]
  [ValidateAntiForgeryToken()]
  public ActionResult CreateForValidation(PersonalDetail model)
  {
      if (ModelState.IsValid)
      {
          if (model.FirstName == null)
          {
              ViewBag.FirstNameError = "Please write first name";
          }
      }

      return View(model);
  }

Now write it at the View wherever we want to display the error.

<p class="text-danger">
      @ViewBag.FirstNameError
</p>

The class attribute written here comes from bootstrap. Similarly, we can use ViewData also instead of ViewBag.

 Views: 19170 | Post Order: 81



Write for us