bootstrap adminlte教程5:文章阅读-显示已阅读的人员名单
我想在打开文章详细,就是内容页上,加上一个已阅读人员的名单。就是哪些用户看过这篇文章。
1.建model。在Model下新建一个ListCMSUser.cs
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace jsdhh2.Models { public class ListCMSUser { [Display(Name = "文章ID")] public string Id { get; set; } public string CmsId {get;set;} /// <summary> /// 名称 /// </summary> [Display(Name = "用户名")] public string UserName { get; set; } /// <summary> /// URL /// </summary> [Display(Name = "头像地址")] public string HeadPicUrl { get; set; } /// <summary> /// URL /// </summary> [Display(Name = "角色名")] public string RoleName { get; set; } /// <summary> /// URL /// </summary> [Display(Name = "阅读日期")] public DateTime CreatTime { get; set; } } }
2。改更上下文。DAL-->OaDALContent.cs
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; using jsdhh2.Models; namespace jsdhh2.DAL { public class OaDALContent : DbContext { public OaDALContent() : base("MyOaContent") { } public DbSet<Menu> Menus { get; set; } public DbSet<MenuRole> MenuRoles { get; set; } public DbSet<CmsColumn> CmsColumns { get; set; } public DbSet<ClubContent> ClubContents { get; set; } public DbSet<CmsContent> CmsContents { get; set; } public DbSet<ListCMSUser> ListCMSUsers { get; set; } } }
3.删除firest code生成的文件。
在控制台输入
Enable-Migrations
然后输入我要的连接字符串
Enable-Migrations -ContextTypeName jsdhh2.DAL.OaDALContent
将右那那个first 生成的文件和false改为True
然后运行显示。注意Add---Table,中间--为我的连接字符DAl
add-migration AddOaDALContentTable
然后更改生成的这个文件201706010526208_AddOaDALContentTable,删除已存在的数据库表
namespace jsdhh2.Migrations { using System; using System.Data.Entity.Migrations; public partial class AddOaDALContentTable : DbMigration { public override void Up() { CreateTable( "dbo.ListCMSUsers", c => new { Id = c.String(nullable: false, maxLength: 128), UserName = c.String(), HeadPicUrl = c.String(), RoleName = c.String(), CreatTime = c.DateTime(nullable: false), }) .PrimaryKey(t => t.Id); } public override void Down() { DropTable("dbo.ListCMSUsers"); } } }
5.然后运行:
PM> update-database
最后一看表,成功的新建了。
6.下面开始实现我的最新看过的用户清单功能了。
1)控制器,在详细方法中
// GET: CmsContents/Details/5 public async Task<ActionResult> Details(string id) { var dt = DateTime.Now; string str = dt.ToString("yyyyMMddHHmmss"); if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } CmsContent cmsContent = await db.CmsContents.FindAsync(id); if (cmsContent == null) { return HttpNotFound(); } string ifrolename = Session["role"].ToString(); if (!cmsContent.CmsPermission.Contains("All")& !cmsContent.CmsPermission.Contains(ifrolename)){ return View("NoYueDu"); } ViewBag.RlpyList1 = ""; ////查询并新建到表ListCMSUser表中。 string username = Session["username"].ToString(); ///取出session的usernme的值 var listcmsusers = (db.ListCMSUsers.Where(x => x.CmsId == id)).ToList(); //if (listcmsusers !=null) if(listcmsusers.Any()) { var xxx = (db.ListCMSUsers.Where(x => x.CmsId == id).Where(z => z.UserName == username)).ToList(); if (xxx.Any()) { ViewBag.ListUsers = listcmsusers; } else { var listCmsUsers = new ListCMSUser(); listCmsUsers.Id = str; listCmsUsers.CmsId= id; listCmsUsers.UserName = username; listCmsUsers.RoleName = ifrolename; listCmsUsers.CreatTime = DateTime.Now; listCmsUsers.HeadPicUrl = Session["headerPic"].ToString(); db.ListCMSUsers.Add(listCmsUsers); db.SaveChanges(); ViewBag.ListUsers = listcmsusers; } } else { var listCmsUsers = new ListCMSUser(); listCmsUsers.Id = str; listCmsUsers.CmsId = id; listCmsUsers.UserName = username; listCmsUsers.RoleName = ifrolename; listCmsUsers.CreatTime = DateTime.Now; listCmsUsers.HeadPicUrl = Session["headerPic"].ToString(); db.ListCMSUsers.Add(listCmsUsers); db.SaveChanges(); ViewBag.ListUsers = listcmsusers; } ///调出所有的回复 /// var rlpyList = (db.CmsContents.Where(x => x.ReplyID == id)).ToList(); if (rlpyList.Any()) { ViewBag.RlpyList = rlpyList; } else { ViewBag.RlpyList1 = "nono"; } return View(cmsContent); }
7.前台
@model jsdhh2.Models.CmsContent @{ ViewBag.Title = "Details"; ViewBag.user11 = Model.CreatUser; } <h2></h2> <div class="col-md-8 col-xs-12"> <div> <h2>@Html.DisplayFor(model => model.Title)</h2> <h2></h2> <div class="panel panel-default"> <div class="panel-heading"> <div class="col-md-2"> <img src=@Html.DisplayFor(model => model.yulou1) class="img-circle" style="width: 46px; height: 46px;"> </div> <div> <a href="">@Html.DisplayFor(model => model.CreatUser) </a> <span style="color:#c7c2c2;">发表于:@Html.DisplayFor(model => model.CreatTime)</span> <br style="padding-left:90px; margin-top:-20px;"> IP地址:@Html.DisplayFor(model => model.PcIp) </br> </div> </div> </div> <div class="dhhtentext" style="min-height:300px;"> @Html.DisplayFor(model => model.Contents) </div> <h2></h2><h2></h2> <div > @if (ViewBag.user11== Session["username"].ToString()) { @Html.ActionLink("编辑", "Edit", new { id = Model.Id }, new { @class = "btn btn-warning" }) } </div> <hr /> <div> @if (ViewBag.RlpyList1 != "nono") { foreach (jsdhh2.Models.CmsContent data1 in (ViewBag.RlpyList as IEnumerable<jsdhh2.Models.CmsContent>)) { <div class="row"> <div class="col-md-2"> <img src=@data1.yulou1 class="img-circle" style="width: 46px; height: 46px;"> </div> <div> <a href="">@data1.CreatUser </a> <br style="padding-left:90px; margin-top:-20px;">发表于:@data1.CreatTime IP地址:@Html.DisplayFor(model => model.PcIp) </div> </div> <div class="dhhtentext"> @data1.Contents </div> <hr /> } } </div> @using (Html.BeginForm("CreateRlpy", "CmsContents", new { cmsId = Model.Id, count = Model.ReplyCount }, FormMethod.Post)) { @Html.AntiForgeryToken() @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-horizontal col-md-12" style="width:100%"> <h4>发表回复</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> <div class="col-md-10"> <textarea name="Contents" required="" lay-verify="required" placeholder="我要回复" class="layui-textarea fly-editor" style="height: 150px;width:100%;" ></textarea> @Html.ValidationMessageFor(model => model.Contents, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="回复" class="btn btn-success" /> </div> </div> </div> } <p> </p> </div></div> <div class="col-md-4 col-xs-12"> <div class="bs-example bs-example-bg-classes" data-example-id="contextual-backgrounds-helpers"> <p class="bg-info titlepadding">已阅读此篇文章的人有:</p> </div> <div class="user"> <ul style="list-style:none;"> @foreach (jsdhh2.Models.ListCMSUser data in (ViewBag.ListUsers as IEnumerable<jsdhh2.Models.ListCMSUser>)) { <li style="text-align:center; float:left;"> <img src=@data.HeadPicUrl class="img-circle" style=" padding:10px;width: 78px; height: 78px;" alt=@data.RoleName> <div>@data.UserName </div> </li> } </ul> </div> </div>
前台我加了回复的textrea 按钮功能。
看效果
然后看一下,回复的方法。
/// <summary> /// 回复的方法 /// </summary> /// <param name="cmsContent"></param> /// <param name="form"></param> /// <returns></returns> [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> CreateRlpy(string cmsId,int count, [Bind(Include = "Contents")]CmsContent cmsRlpy) { var dt = DateTime.Now; string str = dt.ToString("yyyyMMddHHmmss"); cmsRlpy.Id = str; cmsRlpy.ReplyID = cmsId; cmsRlpy.CreatUser = Session["username"].ToString(); cmsRlpy.PcIp = Request.UserHostAddress; cmsRlpy.ReplyCount = count+1; cmsRlpy.CreatTime = DateTime.Now; cmsRlpy.yulou1 = Session["headerPic"].ToString(); cmsRlpy.ColumnId = 0; if (ModelState.IsValid) { db.CmsContents.Add(cmsRlpy); await db.SaveChangesAsync(); return RedirectToAction("Details", "CmsContents",new { id= cmsId }); } return View(cmsRlpy); }
测试,一切正常。
现在需要在详细页面增加对他的调用。