客车网上售票系统项目之留言管理和新闻管理
1、今日完成任务:
(1)留言管理页面设计和代码实现
(2)回复留言、删除留言功能
(3)新闻公告管理-公告类新闻管理的增删改查
(4)新闻公告管理-图片类新闻管理的增删改查
2、核心源码:
留言管理页面代码:
@using OnlineTicketSystem.Models;
@model List<Leaves>
@{
ViewBag.Title = "Index";
}
<script type="text/javascript">
function deleteLeave(lid)
{
$.ajax({
url: '/Leave/DeleteLeave',
type: 'post',
data: { "lid": lid },
dataType: 'json',
success: function (data) {
if (data.code == 1) {
window.location.href = '@Url.Action("Index", "Leave")';
}
else {
alert("删除失败!!!");
}
}
});
}
$(function () {
$("#nav .nav #li3").addClass("active").siblings().removeClass("active");
});
</script>
<br />
<br />
<div class="container">
<form action="/Leave/Index" method="get">
用户名:<input type="text" style="width:200px; height:38px; border-radius:5px;" name="UserName" value="@Request.QueryString["UserName"]" />
留言时间:<input type="date" name="LeaveTime" style="width:200px; height:38px; border-radius:5px;" value="@Request.QueryString["LeaveTime"]" />
<button type="submit" class="btn btn-primary">查询</button>
</form>
<table class="table table-striped m-b-none text-small">
<thead>
<tr>
<th>留言人</th>
<th>留言内容</th>
<th>留言时间</th>
<th>回复内容</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@{
if (Model.Count > 0)
{
foreach (var item in Model)
{
<tr>
<td>@item.UserInfo.UserName</td>
<td>@item.LeaveContent</td>
<td>@item.LeaveTime</td>
<td>@item.ReplyContent</td>
<td>
<button class="btn btn-primary" onclick="javascript:window.location.href='/Leave/ReplyLeave?lid=@item.LeaveID'">回复</button>
<button class="btn btn-primary" onclick="deleteLeave(@item.LeaveID)">删除</button>
</td>
</tr>
}
}
}
</tbody>
</table>
</div>
留言管理台后代码实现(Controller):
OnlineTicketEntities1 db = new OnlineTicketEntities1(); // GET: Leave public ActionResult Index(string UserName, string LeaveTime) { if (Session["userID"] == null) { return RedirectToAction("Index", "Login"); } var list = db.Leaves.ToList(); if (!string.IsNullOrEmpty(UserName)) { list = list.Where(a => a.UserInfo.UserName.Contains(UserName)).ToList(); } if (!string.IsNullOrEmpty(LeaveTime)) { list = list.Where(a => a.LeaveTime.Date == Convert.ToDateTime(LeaveTime)).ToList(); } return View(list); } /// <summary> /// 查看留言内容 /// </summary> /// <param name="lid"></param> /// <returns></returns> [HttpGet] public ActionResult ReplyLeave(int lid) { var c = db.Leaves.Where(a => a.LeaveID == lid).FirstOrDefault().LeaveContent; ViewBag.LeaveContent = c; return View(); } /// <summary> /// 回复留言 /// </summary> /// <param name="LeaveID"></param> /// <param name="ReplyContent"></param> /// <returns></returns> [HttpPost] public ActionResult ReplyLeave(int LeaveID,string ReplyContent) { if (LeaveID!=0 ) { Leaves data = db.Leaves.Where(a => a.LeaveID == LeaveID).FirstOrDefault(); if (!string.IsNullOrEmpty(ReplyContent)) { data.ReplyContent = ReplyContent; } DbEntityEntry<Leaves> entry = db.Entry<Leaves>(data); entry.State = EntityState.Modified; int ares = db.SaveChanges(); if (ares > 0) { return RedirectToAction("Index"); } else { Response.Write("<script>alert('回复失败!!!')</script>"); } } return View(); } /// <summary> /// 删除留言 /// </summary> /// <param name="lid"></param> /// <returns></returns> public ActionResult DeleteLeave(int lid) { int code = 1; Leaves u = db.Leaves.Where(a => a.LeaveID == lid).FirstOrDefault(); DbEntityEntry<Leaves> entry = db.Entry<Leaves>(u); entry.State = EntityState.Deleted; code = db.SaveChanges(); JsonResult ajax = new JsonResult(); ajax.JsonRequestBehavior = JsonRequestBehavior.AllowGet; ajax.Data = new { code = code }; return ajax; }
留言管理页面截图:
图片类新闻管理页面代码:
1 @using OnlineTicketSystem.Models; 2 @model List<News> 3 @{ 4 ViewBag.Title = "新闻管理"; 5 } 6 <script type="text/javascript"> 7 $(function () { 8 $("#nav .nav #li5").addClass("active").siblings().removeClass("active"); 9 }); 10 function deletePhotoNew(nid) 11 { 12 $.ajax({ 13 url: '/News/DeleteAnnNews', 14 type: 'post', 15 data: { "nid": nid }, 16 dataType: 'json', 17 success: function (data) { 18 if (data.code == 1) { 19 window.location.href = '@Url.Action("PhotoNewsIndex","News")'; 20 } 21 else { 22 alert("删除失败!!!"); 23 } 24 } 25 }); 26 } 27 28 </script> 29 <br /> 30 <br /> 31 <div class="container"> 32 <form action="/News/PhotoNewsIndex" method="get"> 33 新闻标题:<input type="text" style="width:200px; height:38px; border-radius:5px;" name="NewsTitle" value="@Request.QueryString["NewsTitle"]" /> 34 发布时间:<input type="date" name="NewsTime" style="width:200px; height:38px; border-radius:5px;" value="@Request.QueryString["NewsTime"]" /> 35 <button type="submit" class="btn btn-primary">查询</button> 36 <button type="button" class="btn btn-primary" onclick="javascript:window.location.href='@Url.Action("AddPhotoNews","News")'">添加</button> 37 </form> 38 <table class="table table-striped m-b-none text-small"> 39 <thead> 40 <tr> 41 <th>新闻封面</th> 42 <th>新闻标题</th> 43 <th>新闻内容</th> 44 <th>添加时间</th> 45 <th>操作</th> 46 </tr> 47 </thead> 48 <tbody> 49 @{ 50 if (Model.Count > 0) 51 { 52 foreach (var item in Model) 53 { 54 <tr> 55 <td> 56 @{ 57 58 if (!string.IsNullOrEmpty(item.ImageUrl)) 59 { 60 <img src="@item.ImageUrl" width="50" height="50" /> 61 } 62 else 63 { 64 <img src="/images/default.jpg" width="50" height="50" /> 65 } 66 } 67 68 </td> 69 <td title="@item.NewsTitle"> 70 @{ 71 72 if (item.NewsTitle.Length > 34) 73 { 74 <span>@item.NewsTitle.Substring(0, 34)...</span> 75 } 76 else 77 { 78 <span>@item.NewsTitle</span> 79 } 80 } 81 </td> 82 <td title="@item.NewsContent"> 83 @{ 84 85 if (item.NewsContent.Length > 34) 86 { 87 <span>@item.NewsContent.Substring(0, 34)...</span> 88 } 89 else 90 { 91 <span>@item.NewsContent</span> 92 } 93 } 94 </td> 95 96 <td>@item.NewsTime</td> 97 <td> 98 <button class="btn btn-success" onclick="javascript:window.location.href='/News/EditPhotoNews?nid=@item.NewsID'">修改</button> 99 <button class="btn btn-info" onclick="deletePhotoNew(@item.NewsID)">删除</button> 100 101 </td> 102 </tr> 103 } 104 } 105 } 106 </tbody> 107 </table> 108 </div>
后台代码Controller实现:
/// <summary> /// 图片类新闻管理 /// </summary> /// <returns></returns> public ActionResult PhotoNewsIndex(string NewsTitle, string NewsTime) { if (Session["userID"] == null) { return RedirectToAction("Index", "Login"); } var list = db.News.Where(a => a.ISImageNew == 1).OrderByDescending(b => b.NewsTime).ToList(); if (!string.IsNullOrEmpty(NewsTitle)) { list = list.Where(a => a.NewsTitle.Contains(NewsTitle)).ToList(); } if (!string.IsNullOrEmpty(NewsTime)) { list = list.Where(a => a.NewsTime.Date == Convert.ToDateTime(NewsTime)).ToList(); } return View(list); } /// <summary> /// 显示图片类新闻 /// </summary> /// <returns></returns> public ActionResult EditPhotoNews(string nid) { News user = null; if (!string.IsNullOrEmpty(nid)) { int uuid = int.Parse(nid); user = db.News.Where(a => a.NewsID == uuid).FirstOrDefault(); } return View(user); } /// <summary> /// 修改图片类新闻 /// </summary> /// <returns></returns> [HttpPost] [ValidateInput(false)] public ActionResult EditPhotoNews(News user) { if (user!=null) { News u = db.News.Where(a => a.NewsID == user.NewsID).FirstOrDefault(); if (!string.IsNullOrEmpty(user.ImageUrl)) { //HttpPostedFileBase file = Request.Files[0]; // string path = Request.MapPath("~/images/NewsImage/") + file.FileName; //file.SaveAs(path); //文件大小(字节数) long fileSize = 0; //重命名的文件名称 string tempName = ""; //物理路径+重命名的文件名称 string fileName = ""; //文件扩展名 string extname = ""; //虚拟路径 string virtualPath = "/images/NewsImage/"; //上传固定路径 string path = Server.MapPath(virtualPath); //上传文件夹名称 string dir = ""; //获取提交的文件 var file = Request.Files[0]; if (file != null && !string.IsNullOrEmpty(file.FileName)) { dir = DateTime.Now.ToString("yyyy-MM-dd"); if (!Directory.Exists(path + dir)) { Directory.CreateDirectory(path + dir); } extname = file.FileName.Substring(file.FileName.LastIndexOf('.'), (file.FileName.Length - file.FileName.LastIndexOf('.'))); tempName = Guid.NewGuid().ToString().Substring(0, 6) + extname; fileName = path + dir + "/" + tempName; fileSize += file.ContentLength; using (FileStream fs = System.IO.File.Create(fileName)) { file.InputStream.CopyTo(fs); fs.Flush(); } } u.ImageUrl = virtualPath + dir + "/" + tempName; } u.ISImageNew = 1; u.NewsContent = user.NewsContent; u.NewsID = user.NewsID; u.NewsTime = DateTime.Now; u.NewsTitle = user.NewsTitle; DbEntityEntry<News> entry = db.Entry<News>(u); entry.State = EntityState.Modified; int data = db.SaveChanges(); if (data > 0) { Response.Write("<script>alert('修改成功!!!')</script>"); return RedirectToAction("PhotoNewsIndex", "News"); } else { Response.Write("<script>alert('修改失败,数据异常!!!')</script>"); } } return View(); }
图片新闻页面截图:
3、遇到的问题:
(1)添加新闻内容的时候即想插入图片,又想插入文字不知道怎么弄
(2)上传新闻封面图片出不来
4、解决的方法:
(1)去网上找了个富文本编辑器KindEdidtor,里面可以写图片也可以写文字
(2)在网上找了个示例,写下来了
5、项目燃尽图更新