一般来说一个网站最主要的部分就是文章,实现了文章功能网站的核心也就出来了。今天开始做文章功能,首先文章应包含以下字段。
字段 |
名称 |
类型 |
必填 |
默认值 |
说明 |
ArtickeId |
文章id |
Int[key] |
是 |
||
CommonModelId |
公共模型Id |
Int |
是 |
||
Source |
来源 |
string(255) |
|||
Author |
作者 |
string(50) |
|||
Intro |
摘要 |
string(255) |
|||
Content |
内容 |
string |
是 |
||
CommonModel |
公共模型数据 |
CommonModel |
virtual |
在Models文件夹点右键添加Article模型
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Ninesky.Models { /// <summary> /// 文章模型 /// </summary> public class Article { [Key] public int ArticleId { get; set; } /// <summary> /// 公共模型id /// </summary> [Display(Name="公共模型编号")] [Required(ErrorMessage="×")] public int CommonModelId { get; set; } /// <summary> /// 来源 /// </summary> [Display(Name="来源")] [StringLength(255, ErrorMessage = "×")] public string Source { get; set; } /// <summary> /// 作者 /// </summary> [Display(Name = "作者")] [StringLength(50, ErrorMessage = "×")] public string Author { get; set; } /// <summary> /// 摘要 /// </summary> [NotMapped] [Display(Name="摘要")] public string Intro { get; set; } /// <summary> /// 内容 /// </summary> [Display(Name="内容")] [Required(ErrorMessage = "×")] [DataType(DataType.Html)] public string Content { get; set; } public virtual CommonModel CommonModel { get; set; } } }
在Repository文件夹右键添加ArticleRepository类,继承自RepositoryBase<Article>。重写Add、update、delete、find函数。
using Ninesky.Models; using System.Linq; namespace Ninesky.Repository { public class ArticleRepository:RepositoryBase<Article> { /// <summary> ///添加文章 /// </summary> /// <param name="article">文章</param> /// <returns></returns> public override bool Add(Article article) { dbContext.Articles.Add(article); return dbContext.SaveChanges() > 0; } /// <summary> /// 更新文章 /// </summary> /// <param name="article">文章</param> /// <returns></returns> public override bool Update(Article article) { dbContext.Articles.Attach(article); dbContext.Entry<Article>(article).State = System.Data.EntityState.Modified; dbContext.Entry<CommonModel>(article.CommonModel).State = System.Data.EntityState.Modified; return dbContext.SaveChanges() > 0; } /// <summary> /// 删除文章 /// </summary> /// <param name="Id">文章id</param> /// <returns></returns> public override bool Delete(int Id) { dbContext.Articles.Remove(dbContext.Articles.SingleOrDefault(a => a.ArticleId == Id)); return dbContext.SaveChanges() > 0; } /// <summary> /// 查找文章 /// </summary> /// <param name="Id">文章id</param> /// <returns></returns> public override Article Find(int Id) { return dbContext.Articles.AsNoTracking().Include("CommonModel").SingleOrDefault(a => a.ArticleId == Id); } } }
在Controllers文件夹上右键添加ArticleController
using Ninesky.Models; using Ninesky.Repository; using System.Web.Mvc; namespace Ninesky.Controllers { public class ArticleController : Controller { ArticleRepository articleRsy; CommonModelRepository cModelRsy; public ArticleController() { articleRsy = new ArticleRepository(); }
}
}
在控制器中添加[UserDefault]action。作为用户登录后文章管理的默认页
/// <summary> /// 用户默认页 /// </summary> [UserAuthorize] public ActionResult UserDefault() { return View(); }
右键添加视图
@{ ViewBag.Title = "文章管理"; Layout = "~/Views/Shared/_User.cshtml"; } <div class="workspace"> <div class="inside"> <div class="notebar"> <img alt="" src="~/Content/Default/User/Images/Icon/Article_16.png" />您现在的位置: 文章管理 </div> </div> </div> <div class="left">@Html.Partial("PartialUserNavMenus")</div> <div class="clear"></div>
F5看下效果
=============================================================
代码随后上传
2013-1-20代码:http://pan.baidu.com/share/link?shareid=204196&uk=2198133459
2013-1-28日5.5“全部文章”代码:http://pan.baidu.com/share/link?shareid=226838&uk=2198133459
2013-2-5日5.7“显示文章”代码http://pan.baidu.com/share/link?shareid=255289&uk=2198133459