1313using System . Collections . Generic ;
1414using System . Linq ;
1515using System . Web . Mvc ;
16+ using System . Web . UI ;
1617using React . Sample . Mvc4 . Models ;
1718using React . Sample . Mvc4 . ViewModels ;
1819
@@ -36,20 +37,21 @@ public class IndexViewModel
3637 {
3738 public IEnumerable < CommentModel > Comments { get ; set ; }
3839 public int CommentsPerPage { get ; set ; }
40+ public int Page { get ; set ; }
3941 }
4042}
4143
4244namespace React . Sample . Mvc4 . Controllers
4345{
44- public class HomeController : Controller
45- {
46- private const int COMMENTS_PER_PAGE = 3 ;
46+ public class HomeController : Controller
47+ {
48+ private const int COMMENTS_PER_PAGE = 3 ;
4749
48- private readonly IDictionary < string , AuthorModel > _authors ;
50+ private readonly IDictionary < string , AuthorModel > _authors ;
4951 private readonly IList < CommentModel > _comments ;
5052
51- public HomeController ( )
52- {
53+ public HomeController ( )
54+ {
5355 // In reality, you would use a repository or something for fetching data
5456 // For clarity, we'll just use a hard-coded list.
5557 _authors = new Dictionary < string , AuthorModel >
@@ -60,36 +62,52 @@ public HomeController()
6062 { "jordwalke" , new AuthorModel { Name = "Jordan Walke" , GithubUsername = "jordwalke" } } ,
6163 { "zpao" , new AuthorModel { Name = "Paul O'Shannessy" , GithubUsername = "zpao" } } ,
6264 } ;
63- _comments = new List < CommentModel >
64- {
65+ _comments = new List < CommentModel >
66+ {
6567 new CommentModel { Author = _authors [ "daniel" ] , Text = "First!!!!111!" } ,
6668 new CommentModel { Author = _authors [ "zpao" ] , Text = "React is awesome!" } ,
6769 new CommentModel { Author = _authors [ "cpojer" ] , Text = "Awesome!" } ,
6870 new CommentModel { Author = _authors [ "vjeux" ] , Text = "Hello World" } ,
6971 new CommentModel { Author = _authors [ "daniel" ] , Text = "Foo" } ,
7072 new CommentModel { Author = _authors [ "daniel" ] , Text = "Bar" } ,
7173 new CommentModel { Author = _authors [ "daniel" ] , Text = "FooBarBaz" } ,
72- } ;
73- }
74+ } ;
75+ }
7476
75- public ActionResult Index ( )
76- {
77- return View ( new IndexViewModel
78- {
79- Comments = _comments . Take ( COMMENTS_PER_PAGE ) ,
80- CommentsPerPage = COMMENTS_PER_PAGE
81- } ) ;
82- }
77+ public ActionResult Index ( )
78+ {
79+ return View ( new IndexViewModel
80+ {
81+ Comments = _comments . Take ( COMMENTS_PER_PAGE ) ,
82+ CommentsPerPage = COMMENTS_PER_PAGE ,
83+ Page = 1
84+ } ) ;
85+ }
8386
84- public ActionResult Comments ( int page )
85- {
86- var comments = _comments . Skip ( ( page - 1 ) * COMMENTS_PER_PAGE ) . Take ( COMMENTS_PER_PAGE ) ;
87+ [ OutputCache ( Duration = 0 , Location = OutputCacheLocation . Any , VaryByHeader = "Content-Type" ) ]
88+ public ActionResult Comments ( int page )
89+ {
90+ Response . Cache . SetOmitVaryStar ( true ) ;
91+ var comments = _comments . Skip ( ( page - 1 ) * COMMENTS_PER_PAGE ) . Take ( COMMENTS_PER_PAGE ) ;
8792 var hasMore = page * COMMENTS_PER_PAGE < _comments . Count ;
8893
89- return Json ( new {
90- comments = comments ,
91- hasMore = hasMore
92- } , JsonRequestBehavior . AllowGet ) ;
93- }
94- }
94+ if ( ControllerContext . HttpContext . Request . ContentType == "application/json" )
95+ {
96+ return Json ( new
97+ {
98+ comments = comments ,
99+ hasMore = hasMore
100+ } , JsonRequestBehavior . AllowGet ) ;
101+ }
102+ else
103+ {
104+ return View ( "Index" , new IndexViewModel
105+ {
106+ Comments = _comments . Take ( COMMENTS_PER_PAGE * page ) ,
107+ CommentsPerPage = COMMENTS_PER_PAGE ,
108+ Page = page
109+ } ) ;
110+ }
111+ }
112+ }
95113}
0 commit comments