bootstrap adminlte教程11:打造内部邮件系统
关于数据表的生成和MODEL的写法,已在上节写了。
1.创建一个带基架的,带EF的Eamil控制器。
整了才发现,我不注意建模型时,竟然 还搞了一个所谓的columnID的字段,这个没用,又是int的,非要赋值,我得在MOdel中删除,然后code first
2. 首写写Create方法
因为大多数字段是后台赋值,象写的时间等,页面上只有3个字段,一是接收人,这个要选择。二是标题,三是内容。
3。在Create 方法中,先要读出所有的用户清单,我是按部门排序,这样,部门这个字段终于有了用。
这需要在控制器中引用USERMANAGER管理方法
private UserManager _userManager; public UserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<UserManager>(); } private set { _userManager = value; } }
我回头看,我的部门是int ,突然发现,前期空余的部门是必须要呀,要不这里太难实现了,OK我们回头做部门。
新建部门的MODE吧。
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace jsdhh2.Models { public class Department { public int Id { get; set; } [Display(Name = "部门名称")] public string DepartmentName { get; set; } public string bei { get; set; } } }
3.修改DAL下的连接上下文
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; } public DbSet<Department> Departments { get; set; } } }
4.然后code first
成功的写入数据库,我现在怕了,怕自己想得不周全,每个表,我都非要加上1-2个bei的字符字段。
5.新建部门的基架 和EF带视图的控制器。Department
然后这个简单,改几个所页面的样式,就不在这写了。edit cteate,index,daitel
然后我们修改User控制器中的Edit方法,先改好GET方法,取出所有的部门列表供选择。
并要在控制器上面加上数据上下文
private OaDALContent db = new OaDALContent();
然后改写后的EDit 的get方法
[HttpGet] public ActionResult Edit(string id) { ///读出所有的部门表的数据。并读为数viewbag ViewBag.drolistmenu = db.Departments.Select(g => new SelectListItem { Text = g.DepartmentName, Value = g.Id.ToString(), Selected = false }); if (string.IsNullOrWhiteSpace(id)) { return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest); } User user = UserManager.FindById(id); if (user == null) { return HttpNotFound(); } var editUserViewModel = new EditUserViewModel() { Id = user.Id, Email = user.Email, PhoneNumber = user.PhoneNumber, WX=user.WX, QQ=user.QQ, DepartmentId=user.DepartmentId, Address=user.Address, Gender= user.Gender, RealName=user.RealName, SpouseId=user.SpouseId, HeaderPic= user.HeaderPic, BirthDate=user.BirthDate, TheHour=user.TheHour, DetailedTime=user.DetailedTime }; return View(editUserViewModel); }
post方法不用改。
然后成功的写入了几个的部门
数据库的表,只有二个部门ID为null,其他都正确的写入了值。
现在联系在邮件前台creat的GET方法中进行更改。
同时需要每个用户名都编辑更改真实名字。
要不邮件内部用,username你看不懂呀。
6.更改Creat Get方法
// GET: Emails/Create public ActionResult Create() { ///首先要读出所有的用户列表,并按部门排序。用于选择接收人,但这里我们要关联把部门名字显出来 //var emailUser = ((from m in UserManager.Users // join mr in db.Departments on m.DepartmentId equals mr.Id // select new EmailUserView { UserName= m.UserName, RealName= m.RealName, DepartmentName= mr.DepartmentName }).OrderBy(z =>z.DepartmentName)).ToList(); var emailuser = (from m in UserManager.Users.OrderBy(z =>z.DepartmentId) select m).ToList(); var emailandDepart = (from m in emailuser join mr in db.Departments on m.DepartmentId equals mr.Id select new EmailUserView { UserName = m.UserName, RealName = m.RealName, DepartmentName = mr.DepartmentName }).ToList(); ViewBag.userLisst = emailandDepart; if (!emailuser.Any()) { ViewBag.userLisst = "nono"; } return View(); }
然后更改视图
@model jsdhh2.Models.Email @{ ViewBag.Title = "Create"; } <div class="dhhheight1 col-md-12 col-xs-12"></div> <!--start 左 --> <div class="col-md-2 hidden-xs"> <div class="box box-solid"> <div class="box-header with-border"> <h3 class="box-title">内部邮箱</h3> <div class="box-tools"> <button type="button" class="btn btn-box-tool" data-widget="collapse"> <i class="fa fa-minus"></i> </button> </div> </div> <div class="box-body no-padding" style="display: block;"> <ul class="nav nav-pills nav-stacked"> <li class="active"> <a href="~/Emails/Index"> <i class="fa fa-inbox"></i> 收件箱 <span class="label label-primary pull-right">12</span> </a> </li> <li><a href="#"><i class="fa fa-envelope-o"></i> 已发邮箱</a></li> <li><a href="~/Emails/Create"><i class="fa fa-file-text-o"></i> 发邮件</a></li> </ul> </div> <!-- /.box-body --> </div> </div> <!--start 右 --> <div class="col-md-10 col-xs-12"> <div class="col-md-3 col-xs-12"> <div class="panel panel-default dhhmargintop"> <div class="panel-heading"> <h3 class="panel-title">选择人员</h3> </div> <div class="panel-body"> @foreach (jsdhh2.ViewModels.EmailUserView data1 in (ViewBag.userLisst as IEnumerable<jsdhh2.ViewModels.EmailUserView>)) { <div> @Html.CheckBox(data1.UserName, false) @data1.DepartmentName: @data1.RealName </div> } </div> <div class="panel-footer">请选择准确哟~ </div> </div> </div> <div class="col-md-9 col-xs-12"> <!-- Content Header (Page header) --> <section class="content-header"> <h1> 发邮件 <small>请注意选择发送对象,同爱护内部环境!</small> </h1> <ol class="breadcrumb"> <li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li> <li class="active">Here</li> </ol> </section> <!--end Content Header (Page header) --> <section class="content"> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> <div class="control-label col-md-2">标题</div> <div class="col-md-10"> @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="control-label col-md-2">内容</div> <div class="col-md-10"> @Html.TextAreaFor(model => model.Contents, htmlAttributes: new { @id = "neirong" }) @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-default" /> </div> </div> </div> } </section> </div> </div> @section Scripts { <script type="text/javascript"> var editor = UE.getEditor('neirong', { //这里可以选择自己需要的工具按钮名称,此处仅选择如下五个 toolbars: [['FullScreen', 'Undo', 'Unlink', 'Redo', 'Link', 'Simpleupload', 'Emotion', 'Unlink', 'Attachment', 'Bold', 'underline', 'forecolor', 'backcolor', 'fontsize', 'justifyleft', 'justifycenter']], //focus时自动清空初始化时的内容 autoClearinitialContent: true, //关闭字数统计 wordCount: false, //关闭elementPath elementPathEnabled: false, //默认的编辑区域高度 initialFrameHeight: 300, //更多其他参数,请参考ueditor.config.js中的配置项 maximumWords:500 }); </script> }
然看看效果。
呵,达到我的要求了,又方便又简洁。
下一步就是控制器了。
// POST: Emails/Create // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关 // 详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。 [HttpPost] [ValidateAntiForgeryToken] [ValidateInput(false)] public ActionResult Create([Bind(Include = "Id,CreatTime,ShowTime,ColumnId,Title,Contents,CreatUser,ReceiveUser,PcIp,EmailState,UserList,HeadPic,bei1,bei2")] Email email, FormCollection form) { //取出二个model的值 var modeltitle = email.Title; var modeContents = email.Contents; //内容是和标题是前台直接送的,现在就差接收的名字了。ReceiveUser字段。 var winnars = from x in form.AllKeys where form[x] != "false" select x; //string xyz = ""; ////我是通过下面的方法来测试写进去没有写进去的是这些无用的__RequestVerificationToken,Title,Contents, //foreach (var id2 in winnars) //{ // xyz = xyz + id2 + ","; //} //email.ReceiveUser = xyz; foreach (var zzs in winnars) { //把这些无用的字符用if全去掉,余下的就全是用户名了。然后接收人等于这个ID,同时进行写入 if (zzs != "Contents" && zzs != "Title" && !zzs.Contains("RequestVerificationToken")) { ////每一个保存都需新来一个ID新生成。key,可下面的写法都有错,他反应太快了,晕死。只能再加上一个id值 //email.Id = DateTime.Now.ToString("yyyyMMddHHmmss"); //需要每次都要new一次 var emailNewModel = new Email(); //下面是基础赋值 emailNewModel.CreatTime = DateTime.Now; emailNewModel.ShowTime = DateTime.Now; emailNewModel.CreatUser = Session["username"].ToString(); emailNewModel.PcIp = Request.UserHostAddress; emailNewModel.EmailState = "0"; emailNewModel.HeadPic = Session["headerPic"].ToString(); string idvar = zzs.ToString()+DateTime.Now.ToString("yyyyMMddHHmmss"); emailNewModel.Id = idvar; //还有二个关键是传进来的model值 emailNewModel.Title = modeltitle; emailNewModel.Contents = modeContents; emailNewModel.ReceiveUser = zzs; db.Emails.Add(emailNewModel); //await db.SaveChangesAsync(); db.SaveChanges(); } } if (ModelState.IsValid) { return RedirectToAction("Index"); } return View(email); }
然后成功。
这里我犯了个过错,MD,我写东西进行,他会按我的时间排序,可是我的时间是前面加了username,所有他自动按ID给我排了,害我的后插入进去的在前面,我一直以为插的都是同一个user的,
断了好久才发现。