I am working on ASP.NET Core 6.0. I have to use TempData in my utility class. So, I am getting it from ITempDataDictionaryFactory as described at How to access TempData in my own utility class? Or TempData is null in constructor.
In my code, I am assigning a List<string> to the TempData. When I try to access it, I am getting a null value error. My code is shown below.
This is how I create and set a List<string> in the tempData:
var tempData = _tempDataDictionaryFactory.GetTempData(httpContext);
if (tempData["InvalidLoginNames"] is null)
{
var InvalidLoginNames = new List<string>();
InvalidLoginNames.Add(username.Trim());
tempData["InvalidLoginNames"] = InvalidLoginNames;
}
This is how I try to get the value out:
var temp = tempData["InvalidLoginNames"] as List<string>;
When I try to get the saved value from temp, it throws error as it is null. Interestingly, when I however over it during debug, it has the value.