zoukankan      html  css  js  c++  java
  • 强类型的HTML模板方法与直接显示模型

    模型:

    public class Article
    {

    [Display(Name="文章")]
    public string ArticleID { get; set; }

    [Display(Name="栏目类别")]
    public string CategoryID { get; set; }

    [Display(Name="标题")]
    [Required]
    [StringLength(50)]
    public string Title { get; set; }

    [Display(Name="正文")]
    [DataType(DataType.MultilineText)]
    public string Content { get; set; }

    [Display(Name = "作者")]
    [StringLength(20)]
    public string AuthorName { get; set; }


    [Display(Name = "发表日期")]
    [DataType(DataType.Date)]      
    [DisplayFormat(DataFormatString="{0:yyyy-MM-dd}",ApplyFormatInEditMode=true)]     只有在视图中配合 @Html.DipspalyFor(a =>a.PostTime )  @Html.EdittorFor(a =>a.PostTime )

    或者模型为IEnumrable<Articles> 遍历IEnumrable模型时 使用 @Html.DipspalyFor(modelItem =>a.PostTime )   才能真正引用到无数据模型,显示为日期格式。  如果在视图中仅仅使用article.PostTime 不会引用数据注解 ,还是会显示 时间
    // [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm:ss}", ApplyFormatInEditMode = true)]
    // [DataType(DataType.DateTime)]
    //[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm:ss}", ApplyFormatInEditMode = true)]
    public DateTime PostTime { get; set; }

    [Display(Name="优先级")]
    [Range(1,100,ErrorMessage="{0}必须在{1}到{2}之间")]
    public int PriorOrder { get; set; }


    [Display(Name="访问量")]
    public int ClickCount { get; set; }


    public virtual Category Category { get; set; }

    public Article()
    {
    this.ArticleID = Guid.NewGuid().ToString();
    }

    控制器:

    public ActionResult List(string categoryID,int? page)
    {
    if (string.IsNullOrEmpty(categoryID))
    return new HttpNotFoundResult();

    ViewBag.CategoryName = _categoryService.Find(c =>c.CategoryID == categoryID).CategoryName;
    ViewBag.CategoryID = categoryID;

    int pageNumber = page ?? 1;
    ViewBag.page = pageNumber;
    int recordPerPage = 20; //设置每一页记录数。
    ViewBag.recordPerpage = recordPerPage;

    int totalRecord = 0; //定义一个输出参数。其实这里给输出参数赋值为0,是没多大意义的。只是为了能够访问到这个变量名而已。输出参数就像引用参数一样,跟形参占用相同的内存空间,形参值改变了,输出参数值也变了。 输出参数是为了解决一个C#中一个函数只能返回一个值的问题,使用它能返回多个值。

    var articleList = _articleService.FindPageListTwoOrder<int,DateTime>(pageNumber, recordPerPage, out totalRecord, a => a.CategoryID == categoryID,OrderType.Asc,a =>a.PriorOrder,OrderType.Desc, a => a.PostTime);
    //var artileList = _articleService.FindAll().Where(a => a.CategoryID == categoryID).OrderBy(a =>a.PriorOrder).OrderByDescending(a =>a.PostTime);

    ViewBag.totalRecord = totalRecord;
    ViewBag.totalPage = (int)Math.Ceiling((double)totalRecord / (double)recordPerPage); //Math.Celling()向上取整函数。 同样,Math.Floor()向下取整函数。


    return View(articleList.ToList());
    }

    视图:

    @model IEnumerable<MajorConstruction.Models.Article>

    @{
    ViewBag.Title = "List";
    }


    <!-- 面包屑导航-->

    @*<ol class="breadcrumb">
    你当前的位置:
    <li><a href="@Url.Action("Index","Home")"><span class="glyphicon glyphicon-home"> 首页</span></a></li>
    <li class="active"><span class="glyphicon glyphicon-th-large"> @ViewBag.CategoryName</span></li>
    </ol>
    *@
    <img src="~/Content/Images/chuangxingfazhan.jpg">
    <div class="page-header"><h3><span class="label label-primary">@ViewBag.CategoryName</span></h3></div>

    <div class="row">
    <div class="col-md-3">

    @{Html.RenderAction("LeftSideMenu", "home", new { area = "", ActiveCategoryID = ViewBag.CategoryID });} <!--//返回一个导航菜单面板 ,通过链接的 RouteValue 传递一个参数数。-->

    @{Html.RenderAction("ClickRangeOnCategory", "home", new { area = "", ActiveCategoryID = ViewBag.CategoryID });} <!--//返回一个本栏目的点击排行-->


    </div>


    <div class="col-md-9">
    <table class="table table-hover table-striped">
    @foreach (var article in Model)
    {
    <tr>
    <td>
    <a href="@Url.Action("ShowArticle", "Article", new { ArticleID = article.ArticleID, area = "" })"><span class="glyphicon glyphicon-file"> @article.Title</span></a>
    </td>
    <td>
    @Html.DisplayFor(modelItem =>article.PostTime)   //强类型的辅助方法会引用模型类的数据注解,显示成日期格式。如果此时使用 @article.PostTime 而不是使用DisplayFor强类型的辅助方法,还是会显示日期时间格式。而不是仅有日期格式。
    </td>
    </tr>


    }
    <tfoot>
    <tr>
    <td class="text-muted" colspan="3">
    每页 @ViewBag.recordPerPage 条记录,共有 @ViewBag.totalRecord 条记录。 第 @(ViewBag.totalRecord == 0 ? 0 : ViewBag.page) 页 ,共 @ViewBag.totalPage 页 @*如果查询到的记录数为0,就显示为第0页。这里有一个条件表达式的目的是为了避免 如 第1页,共0页。的情况。*@
    </td>

    </tr>
    </tfoot>
    </table>


    @if (ViewBag.totalRecord != 0) //是为了避免出现没有记录,还是显示下一页的符号链接。这将会造成程序的一个bug,可以一址点下一页的符号,但是会没有记录。
    {
    <ul class="pagination">
    @if (ViewBag.page != 1) //如果当前页面不是第1页,就显示 <<上一页的符号链接。当前页面是第1页,就不显示<<了。
    {
    <li><a href="@Url.Action("List", new { categoryID=ViewBag.CategoryID, page = (int)(ViewBag.page) - 1 })">&laquo;</a></li> @*为了保证分页与筛选功能的一致性,所以在链接中增加了路由参数。并将当前值通过ViewBag回传给各个输入表单字段。*@
    }

    @for (int page = 1; page <= (int)@ViewBag.totalPage; page++)
    {
    string activeCss = page == (int)ViewBag.page ? "active" : null;
    <li class="@activeCss"><a href="@Url.Action("List", new { categoryID = ViewBag.CategoryID, page = page })">@page</a></li>

    }
    @if (ViewBag.page != ViewBag.totalPage) //如果当前页面不是最后一页了,就显示 >>下一页的符号链接。当前页面是最后一页,就不显示>>了。
    {
    <li><a href="@Url.Action("List", new { categoryID = ViewBag.CategoryID, page = (int)(ViewBag.page) + 1 })">&raquo;</a></li>
    }

    </ul>
    }

    </div>
    </div>

  • 相关阅读:
    第一次冲刺结果演示 一组评审总结
    检查博客情况
    第十次站立会议
    第九次站立会议
    暑期实训day4
    暑期实训day3
    暑期实训day2.1——一发空格引发的血案
    暑期实训day2
    暑期实训day1
    黑板模式
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/4489524.html
Copyright © 2011-2022 走看看