When I access ViewData inside a method in the controller, I am able to assign the value in the form of dictionary ie.
ViewData["message"]="this is a custom message";
but I got into a scenario where I was trying to handle the Exception in MVC, here is my code:
public void OnException(ExceptionContext filterContext)
{
if (!filterContext.ExceptionHandled && (filterContext.Exception is ArgumentOutOfRangeException))
{
filterContext.Result = new ViewResult { ViewName = "Error", ViewData = };
filterContext.ExceptionHandled = true;
}
}
Now when handling exception i would like to pass a message to the error page, so tried to access the Result property of the ExceptionContext. Now
my question is why am I not able to assign a value to the ViewData in a dictionary-like a format here
filterContext.Result = new ViewResult { ViewName = "Error", ViewData = };
This is also a property returning a ViewDataDictionary object, when am I able to assign a value in the Controller method like this ViewData["message"] = "argument exception error"; the why am I not able to do the same inside the ViewResult object.