1

I have saved some rows of data in a list named "viewListNextWeek". I want to send this list to the next razor page, where I'm redirecting to it by

return RedirectToPage("../Food/nextWeekFood");

Based on this, I have tried

HttpContext.Session.Set<List<reserveInfo>>("List", viewListNextWeek);

But I got the error

Error   CS0308  The non-generic method 'ISession.Set(string, byte[])' cannot be used with type arguments    

I also read this, but I don't understand very well what to do.

Nastaran
  • 91
  • 7
  • SO you want to set list of value in session? – Xinran Shen Jan 18 '23 at 07:43
  • You could serlize the object and pass it with TempData when you redirect to another action,the case related :https://stackoverflow.com/questions/7993263/viewbag-viewdata-and-tempdata – Ruikai Feng Jan 18 '23 at 07:47

1 Answers1

1

You can create an extension method to save list of data into session. please refer to this simple demo:

public static class TestSession
    {
        //set session
        public static void SetObjectsession(this ISession session, string key, object value)
        {
            session.SetString(key, JsonConvert.SerializeObject(value));
        }

        //get session
        public static T GetObjectsession<T>(this ISession session, string key)
        {
            var value = session.GetString(key);
            return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
        }
    }

Then use this method to set and get session:

HttpContext.Session.SetObjectsession("A", viewListNextWeek);

HttpContext.Session.GetObjectsession<List<reserveInfo>>("A");
Xinran Shen
  • 8,416
  • 2
  • 3
  • 12