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.
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.
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>
}