冯 海

一个程序新人菜鸟的日记,希望大家多多关照。QQ:32316131

bootstrap adminlte教程10:问答系统的ClubContents的信息管理员的功能

因为问答有涉及公开和不公开。

现在才发现要怎么实现哩,其实最好的方法我觉得是在用户的信息中,给他增加一个字段,是否信息管理员。但又得再次扩充字段。

要么新建一个信息管理员的表?来对应当前角色名,如是存在则返T,不存在则返F,即在搜索信息前加一个总的IF?

好头痛。

 为了方便,我觉得还是新建一个表了,就二个字段,一个ID,一个username,为什么不用UserId哩,我老觉得又要去关联一次,反正UserName我们存在了Session中。

 1.新建模型

我这新建了一个,还把邮箱太详细(内部的象短信内能的一起建了),好FIRST CODE

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace jsdhh2.Models
{
	public class InfoManageUser
	{
		/// <summary>
		/// 菜单ID
		/// </summary>
		[Display(Name = "管理用户id")]
		public string Id { get; set; }
		[Required]
		[Display(Name = "信息员")]
		public string InfoUsers{ get; set; }
		public string bei { get; set; }
	}


}

  

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace jsdhh2.Models
{
	public class Email
	{
		/// <summary>
		/// 菜单ID
		/// </summary>
		[Display(Name = "邮件id")]
		public string Id { get; set; }

		[Display(Name = "创建日期")]
		[DataType(DataType.Date)]
		[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd  HH:mm:ss}")]

		public DateTime CreatTime { get; set; }
		[Display(Name = "查看日期")]
		[DataType(DataType.Date)]
		[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd  HH:mm:ss}")]

		public DateTime ShowTime { get; set; }
		public int ColumnId { get; set; }

		[StringLength(60, MinimumLength = 3, ErrorMessage = "主题的长度必须在3到60个字符")]
		public string Title { get; set; }

		[Required]
		public string Contents { get; set; }
		[Display(Name = "发件人")]
		public string CreatUser { get; set; }
		[Display(Name = "收件人")]
		public string ReceiveUser{ get; set; }
		[Display(Name = "发件人IP")]
		public string PcIp { get;set; }
		[Display(Name = "状态")]
		public string EmailState { get; set; }

		public string UserList { get; set; }
		[Display(Name = "头像")]
		public string HeadPic { get; set; }
	 
		public string bei1 { get; set; }

		public string bei2 { 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; }

		public DbSet<InfoManageUser>  InfoManageUsers{ get; set; }
		public DbSet<Email> Emails { get; set; }
	}


}

  

3.生成解决方案

4.打开控制台

Enable-Migrations

然后:

Enable-Migrations -ContextTypeName jsdhh2.DAL.OaDALContent

 

看见没,二个数据库已成功的 code first

 

5.添加一个基本EF的带视图的控制器。InfoManageUsersController

 

这样做的主要目的是,大部份工作都不需要我们做了。

 6.在后台,用管理员增加菜单管理,同时对Super用户授权。

 7.然后修改样式。

index

edit

详细

删除四个样式。

 然后成功新建二个信息管理员

 7,改写ClubContents的index方法。

先用

///先分析当前用户是否是信息管理员,在infomanageruser表中进行搜索.然后ifuser.count>0
			string varuser = Session["username"].ToString();
			var ifuser =from a in db.InfoManageUsers.Where(x =>x.InfoUsers== varuser)
						select a;
	 
if (ifuser.Count() > 0)
					{}

 上面的方法进行读写,分析是否存在数据。

然后再改写所有的读的LING,都加上一句,如《0,则要creatUser=session[username] 

 public  ActionResult  Index(string ClubState, string listColumn, string SearchString, int? page)

		{
			///先分析当前用户是否是信息管理员,在infomanageruser表中进行搜索.然后ifuser.count>0
			string varuser = Session["username"].ToString();
			var ifuser =from a in db.InfoManageUsers.Where(x =>x.InfoUsers== varuser)
						select a;
	 
			//读出所有的栏目到表中
			ViewBag.drolistmenu = db.CmsColumns.Where(x => x.ColumnType == "2");
			///当没有字符串产生的时候,为PAGE1
			if ( !string.IsNullOrEmpty(ClubState) && !string.IsNullOrEmpty(SearchString) && !string.IsNullOrEmpty(listColumn))
			{
				page = 1;
			}

			ViewBag.listColumn = listColumn;
			ViewBag.ClubState = ClubState;			 
			ViewBag.SearchString = SearchString;
			///获取数据,我们默认显示未完结
			var seachindex = from w in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null).Where(x => x.ClubState == "0")
							 select w;
			
			//再次分析栏目
			///首先看栏目变量是否为空
			if (!string.IsNullOrEmpty(listColumn))
			{
				int clounid = int.Parse(listColumn);
				if (ClubState == "1")
				{
					if (ifuser.Count() > 0)
					{
						seachindex = from l in db.ClubContents.OrderByDescending(q => q.CreatTime).Where(m => m.ReplyID == null).Where(x => x.ClubState == "1").Where(p => p.ColumnId == clounid)
									 select l;
					}
					else
					{
						seachindex = from l in db.ClubContents.OrderByDescending(q => q.CreatTime).Where(m => m.ReplyID == null).Where(x => x.ClubState == "1").Where(p => p.ColumnId == clounid).Where(t =>t.CreatUser== varuser)
									 select l;

					}
				}

				else
				{
					if (ifuser.Count() > 0)
					{
						seachindex = from l in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null).Where(x => x.ClubState == "0").Where(k => k.ColumnId == clounid)
									 select l;
					}
					else
					{
						seachindex = from l in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null).Where(x => x.ClubState == "0").Where(k => k.ColumnId == clounid).Where(t => t.CreatUser == varuser)
									 select l;

					}

				}
			}
			else
			{

				if (ClubState == "1")
				{
					if (ifuser.Count() > 0)
					{
						seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null).Where(x => x.ClubState == "1")
									 select x;
					}
					else
					{
						seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null).Where(x => x.ClubState == "1").Where(t => t.CreatUser == varuser)
									 select x;


					}
				}
				if (ClubState == "all")
				{
					if (ifuser.Count() > 0)
					{
						seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null)
									 select x;
					}
					else
					{
						seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null).Where(t => t.CreatUser == varuser)
									 select x;

					}
				}


			}

			//搜索,如是搜索字符串不为空,则取消所有的,只取Title包含有,同时降序
			if (!string.IsNullOrEmpty(SearchString))
			{
				seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null).Where(x => x.Title.Contains(SearchString))
							 select x;

			}


			int pageNumber = page ?? 1;
			int pageSize =8;
			//通过ToPagedList扩展方法进行分页
			//IPagedList<CmsContent> pagedList = seachindex.ToPagedList(pageNumber, pageSize);


			return View(seachindex.ToPagedList(pageNumber, pageSize));

 
        }

  改写index方法

 

 // GET: ClubContents
        public  ActionResult  Index(string ClubState, string listColumn, string SearchString, int? page)

		{
			///先分析当前用户是否是信息管理员,在infomanageruser表中进行搜索.然后ifuser.count>0
			string varuser = Session["username"].ToString();
			var ifuser =from a in db.InfoManageUsers.Where(x =>x.InfoUsers== varuser)
						select a;
	 
			//读出所有的栏目到表中
			ViewBag.drolistmenu = db.CmsColumns.Where(x => x.ColumnType == "2");
			///当没有字符串产生的时候,为PAGE1
			if ( !string.IsNullOrEmpty(ClubState) && !string.IsNullOrEmpty(SearchString) && !string.IsNullOrEmpty(listColumn))
			{
				page = 1;
			}

			ViewBag.listColumn = listColumn;
			ViewBag.ClubState = ClubState;			 
			ViewBag.SearchString = SearchString;
			///获取数据,我们默认显示未完结
			var seachindex = from w in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null&& m.ClubState == "0")
							 select w;
			
			//再次分析栏目
			///首先看栏目变量是否为空
			if (!string.IsNullOrEmpty(listColumn))
			{
				int clounid = int.Parse(listColumn);
				if (ClubState == "1")
				{
					if (ifuser.Count() > 0)
					{
						seachindex = from l in db.ClubContents.OrderByDescending(q => q.CreatTime).Where(m => m.ReplyID == null&& m.ClubState == "1"&& m.ColumnId == clounid)
									 select l;
					}
					else
					{
						seachindex = from l in db.ClubContents.OrderByDescending(q => q.CreatTime).Where(m => m.ReplyID == null&& m.ClubState == "1"&& m.ColumnId == clounid).Where(t =>t.CreatUser== varuser||t.ClubPermission=="1")
									 select l;

					}
				}

				else
				{
					if (ifuser.Count() > 0)
					{
						seachindex = from l in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null&& m.ClubState == "0"&& m.ColumnId == clounid)
									 select l;
					}
					else
					{
						seachindex = from l in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null&& m.ClubState == "0"&& m.ColumnId == clounid).Where(t => t.CreatUser == varuser || t.ClubPermission == "1")
									 select l;

					}

				}
			}
			else
			{

				if (ClubState == "1")
				{
					if (ifuser.Count() > 0)
					{
						seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null&& m.ClubState == "1")
									 select x;
					}
					else
					{
						seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null&& m.ClubState == "1").Where(t => t.CreatUser == varuser || t.ClubPermission == "1")
									 select x;


					}
				}
				if (ClubState == "all")
				{
					if (ifuser.Count() > 0)
					{
						seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null)
									 select x;
					}
					else
					{
						seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null).Where(t => t.CreatUser == varuser||t.ClubPermission == "1")
									 select x;

					}
				}


			}

			//搜索,如是搜索字符串不为空,则取消所有的,只取Title包含有,同时降序
			if (!string.IsNullOrEmpty(SearchString))
			{
				if (ifuser.Count() > 0)
				{
					seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null&& m.Title.Contains(SearchString))
								 select x;
				}
				else
				{
					seachindex = from x in db.ClubContents.OrderByDescending(c => c.CreatTime).Where(m => m.ReplyID == null&& m.Title.Contains(SearchString)).Where(t => t.CreatUser == varuser || t.ClubPermission == "1")
								 select x;

				}

			}


			int pageNumber = page ?? 1;
			int pageSize =8;
			//通过ToPagedList扩展方法进行分页
			//IPagedList<CmsContent> pagedList = seachindex.ToPagedList(pageNumber, pageSize);


			return View(seachindex.ToPagedList(pageNumber, pageSize));

 
        }

  

 

//  
posted @ 2017-06-04 11:07  秋天来了哟  阅读(284)  评论(0编辑  收藏  举报
认识就是缘份,愿天下人都快乐!
QQ: 32316131
Email: 32316131@qq.com