因爲這個東西的原理沒有什麼難的(只是實現的時候有少許的坑),故直接上代碼以便查閱。另:本文給出的Action附送了點擊量統計。html
public ActionResult SingleNews(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var news = storeDB.articles.Find(id); var prev = storeDB.articles.Where(p => p.ID<id).OrderByDescending(p => p.ID).Take(1).ToList(); var next = storeDB.articles.Where(p => p.ID>id).Take(1).ToList(); if(prev.Count>0) { ViewBag.preTitle = prev.ElementAt(0).title; ViewBag.preId = prev.ElementAt(0).ID; } else { ViewBag.preTitle = "沒有了"; ViewBag.preId = id; } if (next.Count>0) { ViewBag.nextTitle = next.ElementAt(0).title; ViewBag.nextId = next.ElementAt(0).ID; } else { ViewBag.nextTitle = "沒有了"; ViewBag.nextId = id; } //記錄點擊量 if(news!=null) { news.clickCount += 1; storeDB.Entry(news).State = EntityState.Modified; storeDB.SaveChanges(); } return View(news); }
視圖中的相關代碼:spa
<div> <label>上一篇:</label>@Html.ActionLink((string)ViewBag.preTitle, "NewsContent", new { id=ViewBag.pre}) <label>下一篇:</label>@Html.ActionLink((string)ViewBag.nextTitle, "NewsContent", new { id=ViewBag.next}) </div>
本文參考了下面這篇文章並在此基礎上作了改進:http://www.cnblogs.com/denny402/p/3215384.htmlcode