Reputation: 4331
In classic ASP.NET i placed a label on the .aspx page and when an error was thrown, for example by an try ... catch block, i made the label visible and bound the error message to the labels Text
property.
But in MVC this seems not to be possible. All examples which i found in the web are showing how to redirect to an dedicated error view. But i wanted to the error directly on the page where it happens or within a model dialog.
Is this possible and how to do this in MVC (4/5)?
Upvotes: 0
Views: 2560
Reputation: 1627
Pass the error message into a ViewBag and then show message in View.
Controller
public ActionResult Index()
{
try
{
//Do something
}
catch (Exception e)
{
ViewBag.Error = e.Message;
}
return View();
}
View
@if(ViewBag.Error != null)
{
<label>@ViewBag.Error</label>
}
Upvotes: 1