TempData value implicitly cast from List<string> to string[] after RedirectToAction()

Viewed 10386

I am using TempData to allow error message to be displayed after a redirect. For compatibility, I am using a List<string> to add messages into a list of errors in NotificationHelper class:

if (TempData["errorNotification"] == null) {
    TempData["errorNotification"] = new List<string> {
        message
    };
} else {
    var data = (List<string>) TempData["errorNotification"];
    data.Add(message);
    TempData["errorNotification"] = data;
}

Inside the controller, I fill that TempData with an error message and before the return RedirectToAction() call, TempData contains a dictionary record with List<string> (To be exact: System.Collections.Generic.List`1[System.String]) as a value but right after the call, inside Terms() function, the value becomes an array (System.String[]).

[HttpGet]
public IActionResult Terms()
{
    return View();
}

[HttpGet]
public IActionResult Index()
{
    NotificationHelper.AddErrorNotification("error!", TempData);
    return RedirectToAction("Terms");
}

The issue is that in order to use the data, I am casting the type in the view like so:

var errorMessages = TempData["errorNotification"] as List<string>;

and after the conversion I have to do this instead:

var errorMessages = TempData["errorNotification"] as string[];

One of above casts will come back as null, because of expecting a wrong type (depending on if I try to render the view before or after the redirect). Is that a desired behavior of TempData? How can I make sure that the view can expect one specific type being provided?

2 Answers
Related