學用MVC4做網站四:公共模型


網站內可能包含文章、留言、產品等,這些內容都有像標題、發布日期,發布人,所屬欄目……一部分共同數據,把這些數據做個公共模型放到一個單獨模型中。

字段

名稱

類型

必填

默認值

說明

CommonModelId

模型Id

Int[key]

 

 

CategoryId

欄目

Int

 

 

Inputer

用戶

string(255)

 

 

Model

模型名稱

string(50)

Article

 

Title

標題

String(255)

 

 

Hits

點擊

Int

0

 

ReleaseDate

發布日期

Datetime

Now

 

Status

狀態

Int

0

0-待審核;1-正常

PicUrl

首頁圖片

string(255)

 

 

CommentStatus

評論狀態

bool

True

False關閉評論,True-依欄目設置

 

 

 

 

 

 

ContentOrders

內容排序方式

List<SelectListItem>

未映射

 

0-默認設置;1-id降序;2-id升序;3-發布時間降序;4-發布時間升序;5-點擊降序,6-點擊升序

Category

欄目

Category

外鍵

 

 

在Modles文件夾點右鍵添加類CommonModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Mvc;

namespace Ninesky.Models
{
    /// <summary>
    /// 公共模型
    /// </summary>
    public class CommonModel
    {
        [Key]
        public int CommonModelId { get; set; }
        /// <summary>
        /// 欄目Id
        /// </summary>
        [Display(Name="欄目")]
        [Required(ErrorMessage="×")]
        public int CategoryId { get; set; }
        /// <summary>
        /// 錄入者
        /// </summary>
        [Display(Name="錄入者")]
        [Required(ErrorMessage = "×")]
        [StringLength(255, ErrorMessage = "×")]
        public string Inputer { get; set; }
        /// <summary>
        /// 模型名稱
        /// </summary>
        [Display(Name="模型名稱")]
        [Required()]
        [StringLength(50)]
        public string Model { get; set; }
        /// <summary>
        /// 標題
        /// </summary>
        [Display(Name="標題")]
        [Required(ErrorMessage = "×")]
        [StringLength(255, ErrorMessage = "×")]
        public string Title { get; set; }
        /// <summary>
        /// 點擊
        /// </summary>
        [Display(Name = "點擊")]
        [Required(ErrorMessage = "×")]
        public int Hits { get; set; }
        /// <summary>
        /// 發布日期
        /// </summary>
        [Display(Name = "發布日期")]
        [Required(ErrorMessage = "×")]
        public DateTime ReleaseDate { get; set; }
        /// <summary>
        /// 狀態【0-待審核;1-正常】
        /// </summary>
        [Display(Name="狀態")]
        [Required(ErrorMessage = "×")]
        public int Status { get; set; }
        /// <summary>
        /// 首頁圖片
        /// </summary>
        [Display(Name="首頁圖片")]
        [StringLength(255, ErrorMessage = "×")]
        public string PicUrl { get; set; }
        /// <summary>
        /// 評論狀態
        /// </summary>
        [Display(Name="評論狀態")]
        [Required(ErrorMessage = "×")]
        public bool CommentStatus { get; set; }
        /// <summary>
        /// 欄目
        /// </summary>
        public virtual Category Category { get; set; }

        public CommonModel()
        {
            ReleaseDate = System.DateTime.Now;
        }
        [NotMapped]
        public static List<SelectListItem> ContentOrders
        {
            get
            {
                List<SelectListItem> _cOrders = new List<SelectListItem>(7);
                _cOrders.Add(new SelectListItem { Text = "默認排序", Value = "0" });
                _cOrders.Add(new SelectListItem { Text = "Id降序", Value = "1" });
                _cOrders.Add(new SelectListItem { Text = "Id升序", Value = "2" });
                _cOrders.Add(new SelectListItem { Text = "發布時間降序", Value = "3" });
                _cOrders.Add(new SelectListItem { Text = "發布時間升序", Value = "4" });
                _cOrders.Add(new SelectListItem { Text = "點擊降序", Value = "5" });
                _cOrders.Add(new SelectListItem { Text = "點擊升序", Value = "6" });
                return _cOrders;
            }
        }
        [NotMapped]
        public static List<SelectListItem> ContentStatus
        {
            get
            {
                List<SelectListItem> _cStatus = new List<SelectListItem>(2);
                _cStatus.Add(new SelectListItem { Text = "待審核", Value = "0" });
                _cStatus.Add(new SelectListItem { Text = "正常", Value = "1" });
                return _cStatus;
            }
        }
    }
}

 

在Repository文件夾點右鍵添加類CommonModelRepository

using Ninesky.Models;
using System.Linq;
using System.Web.Mvc;

namespace Ninesky.Repository
{
    public class CommonModelRepository:RepositoryBase<CommonModel>
    {
        public override bool Add(CommonModel cModel)
        {
            dbContext.CommonModels.Add(cModel);
            return dbContext.SaveChanges() > 0;
        }
        public override bool Update(CommonModel cModel)
        {
            dbContext.CommonModels.Attach(cModel);
            dbContext.Entry<CommonModel>(cModel).State = System.Data.EntityState.Modified;
            return dbContext.SaveChanges() > 0;
        }
        public override bool Delete(int cModelId)
        {
            dbContext.CommonModels.Remove(dbContext.CommonModels.SingleOrDefault(m => m.CommonModelId == cModelId));
            return dbContext.SaveChanges() > 0;
        }
        /// <summary>
        /// 獲取分頁公共模型內容列表
        /// </summary>
        /// <param name="categoryId">欄目Id</param>
        /// <param name="cChildren">是否包含子欄目</param>
        /// <param name="model">模型名稱</param>
        /// <param name="userName">用戶名</param>
        /// <param name="currentPage">當前頁</param>
        /// <param name="pageSize">每頁記錄數</param>
        /// <param name="order">排序方式</param>
        /// <returns>分頁數據</returns>
        public PagerData<CommonModel> List(int categoryId, bool cChildren,string model, string userName, int currentPage, int pageSize, int order)
        {
            PagerConfig _pConfig = new PagerConfig { CurrentPage = currentPage, PageSize = pageSize };
            var _cModels = dbContext.CommonModels.Include("Category").AsQueryable();
            if (categoryId != 0)
            {
                if (cChildren)//包含子欄目
                {
                    CategoryRepository _cRsy = new CategoryRepository();
                    IQueryable<int> _children = _cRsy.Children(categoryId, 0).Select(c => c.CategoryId);
                    _cModels = _cModels.Where(m => _children.Contains(m.CategoryId));
                }
                else _cModels = _cModels.Where(m => m.CategoryId == categoryId);//不包含子欄目
            }
            if (!string.IsNullOrEmpty(model)) _cModels = _cModels.Where(m => m.Model == model);
            if (!string.IsNullOrEmpty(userName))_cModels = _cModels.Where(m => m.Inputer == userName);
            _pConfig.TotalRecord = _cModels.Count();//總記錄數
            //排序
            switch (order)
            {
                case 1://id降序
                    _cModels = _cModels.OrderByDescending(m => m.CommonModelId);
                    break;
                case 2://Id升序
                    _cModels = _cModels.OrderBy(m => m.CommonModelId);
                    break;
                case 3://發布日期降序
                    _cModels = _cModels.OrderByDescending(m => m.ReleaseDate);
                    break;
                case 4://發布日期升序
                    _cModels = _cModels.OrderBy(m => m.ReleaseDate);
                    break;
                case 5://點擊降序
                    _cModels = _cModels.OrderByDescending(m => m.Hits);
                    break;
                case 6://點擊升序
                    _cModels = _cModels.OrderBy(m => m.Hits);
                    break;
                default://默認id降序
                    _cModels = _cModels.OrderByDescending(m => m.CommonModelId);
                    break;
            }
            //分頁
            _cModels = _cModels.Skip((_pConfig.CurrentPage - 1) * _pConfig.PageSize).Take(_pConfig.PageSize);
            PagerData<CommonModel> _pData = new PagerData<CommonModel>(_cModels, _pConfig);
            return _pData;
        }
    }
}

函數的意義 List(int categoryId, bool cChildren,string model, string userName, int currentPage, int pageSize, int order)用來獲取分頁公共模型內容列表

參數:categoryId-欄目Id;id-欄目id【id=0全部欄目】;cChildren-是否包含子欄目【id!=0時有效】;userName –用戶名【所有用戶為空】; page-當前頁;pazeSize-每頁顯示記錄數(0- 表示依欄目設置);order排序方式

返回數據類型:分頁數據PagerData<CommonModel>

公共模型到此,后面開始寫文章功能。

=======================================

去黑龍江了一段時間,真的冷!十幾天沒刮胡子、沒洗澡,不是人過的日子,好在終於弄的差不多了,我王老五又殺回來了!收心、學習!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM