zoukankan      html  css  js  c++  java
  • asp.net MVC实现文章的“上一篇下一篇”

    由于这个东西的原理没有什么难的(只是实现的时候有少量的坑),故直接上代码以便查阅。另:本文给出的Action附送了点击量统计。

    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);
            }

    视图中的相关代码:

    <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.html

  • 相关阅读:
    .NET中获取系统硬件信息
    TTF文件的制作——打造属于自己的字体
    HDU4415 Assassin’s Creed
    HDU4193 Nonnegative Partial Sums(单调队列)
    HDU4414 Finding crosses
    HDU4407 Sum
    HDU4403 A very hard Aoshu problem
    HDU4417 Super Mario
    HDU4419 Colourful Rectangle
    非递归快速排序和非递归快速乘幂
  • 原文地址:https://www.cnblogs.com/eternalwt/p/3678719.html
Copyright © 2011-2022 走看看