In an ASP.NET MVC app, I have a controller action which calls business layer to add an entity.
If for business reasons, the entity could not be added, an Error property is set to true along with a description.
If true, I set a TempData
key to the error message and then redirect to a view which has code to display the error stored in TempData
if it exists. Sometimes the conditional block is shown and sometimes not.
Here is the relevant code in my controller
var added = ModelHelper.CreateSessionCode(model);
if(added.Error)
{
TempData["SessionCodesMessage"] = model.ErrorDescription;
TempData["MessageClass"] = "alert-danger";
}
else
{
TempData["SessionCodesMessage"] = "Created session code";
TempData["MessageClass"] = "alert-success";
}
return RedirectToAction("Index");
Then in my view I have this code:
@if (TempData["SessionCodesMessage"] != null)
{
<div class="alert @TempData["MessageClass"] alert-dismissable" style="margin-top: 8px;">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
@(TempData["SessionCodesMessage"])
</div>
}
And it seems to be inconsistent when the message is displayed or not. Could this be a browser cache issue or similar? From stepping through the code I can confirm that the execution goes into both controller conditional blocks depending on the result of adding the entity.