/// <summary> /// 导航分部视图 /// </summary> /// <param name="category">分类</param> /// <returns>分部视图</returns> public PartialViewResult Category(string category = null) { //保存选择的分类 ViewBag.SelectedCategory = category; List <CategoryInfo> categoryInfos = new List <CategoryInfo>(); int catecount = 0; //获取各个分类的总数 using (var db = new TreasureDataContext()) { var treas = db.Treasures.Where(t => (category == null || t.TreasureType == category) && (t.Cover != null && t.DetailPic != null) && (t.DLogUID == null)); //获取所有的分类 IEnumerable <string> categorise = treas .Select(x => x.TreasureType) .Distinct() .OrderBy(x => x); //保存全部分类的信息 categoryInfos.Add(new CategoryInfo { CateCount = treas.Count(), CateName = "所有物品", CateLink = "/Treasure/List" }); foreach (var cate in categorise) { catecount = treas.Where(x => x.TreasureType == cate).Count(); CategoryInfo categoryInfo = new CategoryInfo { CateName = cate, CateCount = catecount, CateLink = "/Treasure/List/" + cate }; categoryInfos.Add(categoryInfo); } } IEnumerable <CategoryInfo> AllCateInfos = categoryInfos; return(PartialView(AllCateInfos)); }
/// <summary> /// 清空封面图和细节图 /// </summary> /// <param name="treaguid"></param> /// <returns>True代表删除成功</returns> public static bool DeletePic(Guid treaguid) { using (var db = new TreasureDataContext()) { var treasure = db.Treasures.Where(t => t.TreasureUID == treaguid).FirstOrDefault(); if (treasure != null) { try { treasure.Cover = null; treasure.DetailPic = null; db.SubmitChanges(); return(true); } catch { return(false); } } return(false); } }
/// <summary> /// 封面上传 /// </summary> /// <returns></returns> public ActionResult Cover() { int count = 0; string msg = ""; bool isSavedSuccessfully = true; var userID = Request.Params["UserUID"]; var treasureUID = Request.Params["TreasureUID"]; string fName = ""; string ImageSrc = ""; //遍历文件 try { foreach (string fileName in Request.Files) { //获取文件对象 HttpPostedFileBase file = Request.Files[fileName]; //保存文件 fName = file.FileName; //文件不为空,长度大于0 if (file != null && file.ContentLength > 0 && file.ContentLength < 2097152) { //储存路径 DirectoryInfo originalDirectory = new DirectoryInfo(string.Format("{0}images\\Treasure", Server.MapPath(@"\"))); //组合字符串成新的路径 string pathString = Path.Combine(originalDirectory.ToString(), userID, treasureUID); //返回路径的文件名和扩展名 var fileFullName = Path.GetFileName(file.FileName); //组成相对路径,用于存入数据库显现图片 ImageSrc = "/images/Treasure" + "/" + userID + "/" + treasureUID + "/" + fileFullName; //获取该路径是否已经存在 bool isExists = Directory.Exists(pathString); //不存在则新建 if (!isExists) { Directory.CreateDirectory(pathString); } //完整文件路径 var path = string.Format("{0}\\{1}", pathString, file.FileName); //保存文件 file.SaveAs(path); } else { isSavedSuccessfully = false; } count++; break; } //路径保存到数据库 using (var db = new TreasureDataContext()) { var treasure = db.Treasures.Where(t => t.TreasureUID == Guid.Parse(treasureUID) && t.HolderID == userID).FirstOrDefault(); if (treasure != null) { treasure.Cover = ImageSrc; db.SubmitChanges(); } else { return(View("Error")); } } } catch (Exception ex) { msg = ex.Message; isSavedSuccessfully = false; } return(Json(new { Result = isSavedSuccessfully, Message = msg, Count = count })); }