Store complex object in TempData

后端 未结 3 791
攒了一身酷
攒了一身酷 2020-11-29 01:16

I\'ve been trying to pass data to an action after a redirect by using TempData like so:

if (!ModelState.IsValid)
{
    TempData[\"ErrorMessages\"] = ModelSta         


        
3条回答
  •  孤城傲影
    2020-11-29 01:43

    I can not comment but I added a PEEK as well which is nice to check if there or read and not remove for the next GET.

    public static T Peek(this ITempDataDictionary tempData, string key) where T : class
    {
        object o = tempData.Peek(key);
        return o == null ? null : JsonConvert.DeserializeObject((string)o);
    }
    

    Example

    var value = TempData.Peek("key") where value retrieved will be of type ClassA
    

提交回复
热议问题