// for testing public void addSome(string content) { BBSItem bi = new BBSItem(); bi.ParentID = -1; bi.BBS = (from b in CQGJ.BBS where b.BBSID == 1 select b).First(); bi.TypeID = 0; bi.Content = content; CQGJ.AddToBBSItem(bi); CQGJ.SaveChanges(); }
/// <summary> /// 创建一个新的BBS Item /// </summary> /// <param name="subject">subject of Item</param> /// <param name="content">content of Item</param> /// <param name="parentId">parent id of Item</param> /// <param name="status">status of Item</param> /// <param name="typeId">type id of Item</param> /// <param name="userId">user id of Item</param> /// <param name="userName">user name of Item</param> /// <param name="bbsId">id of Item belonged bbs</param> /// <param name="attachmentId">attachment id of Item</param> public ActionResult CreateItem(string subject, string content, int parentId, int status, int typeId, int userId, string userName, int bbsId, int attachmentId) { BBSItem newBBSItem = new BBSItem(); // 因为是新帖,无论是发起帖,还是回帖,都没有LastReplyTime,所以设为null newBBSItem.LastReplyTime = null; newBBSItem.ParentID = parentId; newBBSItem.ReplyCount = 0; newBBSItem.Status = status; newBBSItem.Subject = subject; newBBSItem.Content = content; newBBSItem.SubmitTime = DateTime.Now; newBBSItem.TypeID = typeId; newBBSItem.UserName = userName; newBBSItem.ViewCount = 0; try { // 因为BBSItem表中BBSID和外键是以BBS对象保存的, // 所以要为BBSItem构造整个BBS对象才能在数据库中保存BBSItem? //BBSItem.BBS.BBSID = Request.Form["bbsid"];错误? newBBSItem.BBS = (from b in CQGJ.BBS where b.BBSID == bbsId select b).First(); // 需要对 newBBSItem.User 实例化,否则会报错 newBBSItem.User = (from u in CQGJ.User where u.UserID == userId select u).First(); } catch (ArgumentNullException) { Response.Write("该BBS论坛已经被删除"); } catch (Exception ex) { Response.Write(ex.Message); } // 需要对 newBBSItem.Attachment 实例化,否则会报错 //newBBSItem.Attachment = (from attachment in CQGJ.Attachment where attachment.AttachmentID == attachmentId select attachment).First(); CQGJ.AddToBBSItem(newBBSItem); CQGJ.SaveChanges(); return null; }
/// <summary> /// 显示上一个帖子 /// </summary> /// <param name="id">本帖ID</param> public ActionResult PreItem(int id) { BBSItemListViewData BBSLVD = new BBSItemListViewData(); int bbsId = GetCurrentBBSID(); try { List<BBSItem> items = (from bi in CQGJ.BBSItem where bi.ParentID == -1 && bi.BBS.BBSID == bbsId select bi).ToList(); BBSItem nextItem = new BBSItem(); for (int i = 0; i < items.Count; i++) { if (items[i].ItemID == id) { if (i != 0) { nextItem = items[i - 1]; break; } else { nextItem = items[i]; break; } } } BBSLVD = GetItemDetailInfoByID(nextItem.ItemID); } catch (Exception ex) { Response.Write("这是第一个帖子" + ex.Message); } return RedirectToAction("item/" + BBSLVD.TopicItemInfo.TopicBBSItem.ItemID + "/1"); }
/// <summary> /// 发起一个新的BBS话题 /// </summary> public ActionResult CreateTopicItem() { BBSItem BBSItem = new BBSItem(); User user = GetUser(); string subject = GetString("subject"); string content = GetString("BBSItemContent"); // 发起帖parentId为-1 int parentId = -1; //status为备用字段,暂时设置为0 int status = 0; //TypeID暂时设置为0 int typeId = 0; // 取得登陆用户ID,userId int userId = user.UserID; // 取得用户姓名 string userName = user.Username; int bbsId = GetInt("bbsid"); // 取得上传附件组的ID, 1表示没有附件 int attachmentId = 1; // 保存文件的路径 string strAbsolutePath = this.HttpContext.Request.MapPath(@"../Content/UploadFiles/BBSFiles/"); // 上传附件, 并获取附件组的ID try { // 获取上传附件组 HttpFileCollectionBase uploadFiles = this.HttpContext.Request.Files; // 创建一个附件组 Attachment at = new Attachment(); if (uploadFiles.Count > 1) { // ?好像数据库中存在数据的情况下才可以使用orderby语句, 否则会报错 // 所以需要对Attachment数据库初使化 attachmentId = (from attachment in CQGJ.Attachment orderby attachment.AttachmentID descending select attachment.AttachmentID).First() + 1; at.AttachmentID = attachmentId; // 在数据库中加一个附件组信息 CQGJ.AddToAttachment(at); } at.TypeID = 0; // 暂时设为0 // 获取新建的附件组的ID //attachmentId = at.AttachmentID; for (int j = 1; j < uploadFiles.Count; j++) { // 创建一个附件 AttachmentItem ai = new AttachmentItem(); ai.AddTime = DateTime.Now; ai.Status = 0; // 暂时设置为0 ai.UserID = userId; ai.TotalSize = uploadFiles[j].ContentLength; ai.Attachment = at; // 上传的文件是用原名呢,还是生成一个名字呢? //ai.FileName = uploadFiles[j].FileName; ai.FileName = FileUpload.SaveFile(uploadFiles[j], strAbsolutePath); // 在数据库中加一个附件信息 CQGJ.AddToAttachmentItem(ai); CQGJ.SaveChanges(); } } catch (HttpException) { // 网络异常 } catch (InvalidOperationException) { Response.Write("错误操作!"); } CreateItem(subject, content, parentId, status, typeId, userId, userName, bbsId, attachmentId); // count 为数据库中发帖总数 int count = (from bi in CQGJ.BBSItem where bi.ParentID == -1 && bi.BBS.BBSID == bbsId select bi).Count(); // page 为当前发帖的页码 int page = count / 10; if (count % 10 != 0) { page = count / 10 + 1; } // 返回BBS列表页面 return RedirectToAction("ListBBSItems/" + bbsId + "/" + page); }
/// <summary> /// 创建一个回帖的BBS Item /// </summary> public ActionResult CreateReplyItem() { BBSItem BBSItem = new BBSItem(); // 获取登陆用户信息 User user = GetUser(); string subject = GetString("subject"); string content = GetString("replyContent"); int parentId = GetInt("parentid"); //status为备用字段,暂时设置为0 int status = 0; //TypeID暂时设置为0 int typeId = 0; // 取得登陆用户ID,userId int userId = user.UserID; // 取得用户姓名 string userName = user.Username; int bbsId = GetInt("bbsid"); // 取得上传附件组的ID,1表示没有附件 int attachmentId = 1; CreateItem(subject, content, parentId, status, typeId, userId, userName, bbsId, attachmentId); BBSItem topic = null; try { // 回复次数加1 topic = (from topic1 in CQGJ.BBSItem where topic1.ItemID == parentId select topic1).First(); topic.ReplyCount = topic.ReplyCount + 1; CQGJ.SaveChanges(); } catch (ArgumentNullException) { Response.Write("该帖子已被删除!"); } if (topic == null) { // 如果被回复的帖子为null,即帖子被删除,返回BBSItem列表 return RedirectToAction("ListBBSItems/" + bbsId + "/1"); } else { // 回复成功,跳转至BBSItem详细信息页面 return RedirectToAction("Item/" + topic.ItemID + "/1"); } }