0

I want to handle following errors using TempData:

1) My custom Error defined by me if certain condition is not fulfilled.

2) To display the exact SQL-server error.

Note: I am Using Redirect after the code.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
kadeer khan
  • 41
  • 1
  • 6

1 Answers1

0

May be you need like this: In the controller

public ActionResult SaveProduct(Product model)
   {
      var ErrorString = null;

      // your custom validations for example,
      if(model.Name == null) ErrorString = "Name is empty";

      try
      {
         // your db save operations 
      }
      catch (Exception exception)
      {
         ErrorString = exception.Message;
      }

      if(ErrorString != null) TempData["Error"] = ErrorString;
      return Redirect("YourAction"); 
    }

And in View:

@{
    var error = (string)TempData["Error"];
}


@if (!string.IsNullOrEmpty(error))
    {
       <p>@Html.Raw(error)</p>
    }
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
Jeyhun Rahimov
  • 3,769
  • 6
  • 47
  • 90