private List <Post> GetPostsFromSession() { try { List <Post> posts = new List <Post>(); object json; TempData.TryGetValue("blogPosts", out json); if (json == null || string.IsNullOrEmpty(json.ToString())) { posts = postsService.GetAll(); TempData["blogPosts"] = (posts == null ? "" : CommonConverter.SerializeObject(posts)); } else { posts = CommonConverter.DeserializeObject <List <Post> >(json.ToString()); } if (posts == null) { posts = new List <Post>(); } return(posts); } catch (Exception ex) { return(new List <Post>()); } }
public ActionResult Index() { object json = null; TempData.TryGetValue("blogPosts", out json); var posts = new List <Post>(); if (json == null || string.IsNullOrEmpty(json.ToString())) { posts = postService.GetAll(); } else { posts = CommonConverter.DeserializeObject <List <Post> >(json.ToString()); } TempData["blogPosts"] = (posts == null ? "" : CommonConverter.SerializeObject(posts)); return(View(posts)); }