Reputation: 11284
I am completely baffled by this s I have never came across this. I have a shared view called "Error" which outputs a standard message followed by a custom message:
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<h2>
Sorry, an error occurred while processing your request. @{ ViewData["ErrorMessage"].ToString(); }
</h2>
Inside a controllers catch block I am setting the ViewData to the custom message:
catch (Exception ex)
{
...
ViewData["ErrorMessage"] = "This is my custom message";
return View("Error");
}
However, when the view is loaded, the ViewData shows my key "ErrorMessage" but never outputs the string.
Upvotes: 2
Views: 1395
Reputation: 139758
Your expression does not display anything because you don't write out the ViewData["ErrorMessage"]
to the response.
With the @{ ... }
you create a razor code block which does not write anything to the output just executes the code what is inside.
To write to the output you need to use the @
sign:
<h2>
Sorry, an error occurred while processing your request. @ViewData["ErrorMessage"]
</h2>
More info about the razor syntax.
Upvotes: 3