zoukankan      html  css  js  c++  java
  • MVC系列15-首页

    1.在ViewModels下新建类IndexViewModel

    public class IndexViewModel

        {

    public List<Models.Account> Accounts { get; set; }

    public List<Models.Article> Articles { get; set; }

        }

    2.编辑ArticleController下的Index

    public ActionResult Index()

            {

                ViewModels.IndexViewModel ivm = new ViewModels.IndexViewModel();

                ivm.Accounts = db.Accounts.OrderByDescending(a => a.ID).Skip(0).Take(6).ToList();

    //0表示从第0个起 取前10个数据 按照点击量倒序排的序

                ivm.Articles= db.Articles.OrderByDescending(a => a.clickCount).Skip(0).Take(10).ToList();

    return View(ivm);

            }

    3.编辑Index的视图

    @model MVCDemo.ViewModels.IndexViewModel

    @{

        ViewBag.Title = "Index";

        Layout = "~/Views/Shared/MyLayout.cshtml";

    }

    @section HeaderSection{

    <link href="~/Content/Site.css" rel="stylesheet" />

    }

    @section ContentBody{

    <h2>Index</h2>

    <div class="row body-content">

    <div class="col-md-4">

    <ul class="list-group">

    <li class="list-group-item">最新用户</li>

    @foreach (MVCDemo.Models.Account item in Model.Accounts)

                    {

    <li class="list-group-item">

    @Html.ActionLink(@item.Email, "IndexList", new { id = item.ID })

    </li>

                    }

    </ul>

    </div>

    <div class="col-md-8">

    <h4>最热文章</h4>

    <hr />

    @foreach (MVCDemo.Models.Article item in Model.Articles)

                {

    <div class="panel panel-default">

    <div class="panel-heading">标题:@Html.ActionLink(@item.topic, "Detail", new { id = item.ID })</div>

    <div class="panel-body">

    @Html.Raw(@item.content.Substring(0, (@item.content.Length >= 100) ? 100 : @item.content.Length))

    </div>

    <div class="panel-footer">

    @Html.ActionLink(@item.Account.Email, "IndexList", new { id = item.AccountID })

                        作者: 创建时间:@item.createTime 点击率:@item.clickCount

    </div>

    </div>

                }

    </div>

    </div>

    }

  • 相关阅读:
    poj 3669 Meteor Shower
    poj 3009 Curling 2.0
    poj 1979 Red and Black
    区间内素数的筛选
    九度oj 题目1347:孤岛连通工程
    poj 3723 Conscription
    poj 3255 Roadblocks
    Luogu P3975 [TJOI2015]弦论
    AT2165 Median Pyramid Hard 二分答案 脑洞题
    后缀自动机多图详解(代码实现)
  • 原文地址:https://www.cnblogs.com/lingr/p/5563490.html
Copyright © 2011-2022 走看看