public ActionResult MySellProduct() { //1. 先判断用户是否登录了 if (!IsLogined()) { return(Content("<script>alert('请登录');window.location.href='/Users/Login'</script>")); } //通过Session获取id,不能通过url地址来获取 string id = Session["UserId"].ToString(); //2.对id进行合法性验证 int userid; if (!int.TryParse(id, out userid)) { return(Content("<script>alert('参数不合法');window.location.href='/Users/Login'</script>")); } //3. 查询订单表,通过卖家id来判断,还添加了评价 using (ShoppingEntities dc = new ShoppingEntities()) { List <Dictionary <String, Object> > sellList = new List <Dictionary <String, Object> >(); var tradeList = dc.TradeRecord.Where(tra => tra.UserSellID == userid).ToList(); foreach (var trade in tradeList) { sellList.Add(ProductObjectToJson.Convert(trade.Product, trade)); } Dictionary <String, Object> retJson = new Dictionary <String, Object>(); retJson.Add("total", sellList.Count()); retJson.Add("products", sellList); return(Json(retJson, JsonRequestBehavior.DenyGet)); } }
/// <summary> /// 返回指定数量的Product对象 /// </summary> /// <param name="categoryId">分类id</param> /// <param name="start">第几条开始</param> /// <param name="count">获取多少条记录</param> /// <param name="total">数据库的记录总数</param> /// <returns></returns> private List <Dictionary <String, Object> > ListProduct(int categoryId, ref int start, int count, ref int total) { int lastPage = 0; using (ShoppingEntities entity = new ShoppingEntities()) { List <Product> products = null; //若分类ID大于0时候,就查询对应分类ID的所有商品数据 if (categoryId > 0) { //返回分类后的产品总数, total = entity.Product.Count(p => p.ProIsSell == false && p.ProCateg == categoryId); lastPage = total / count + 1; products = (from c in entity.Category join p in entity.Product on c.CategID equals p.ProCateg where c.CategID == categoryId && p.ProIsSell == false orderby p.ProID descending select p ).Skip((start - 1) * count).Take(count).ToList(); } //否则就查询所有商品显示在最新栏内容里面 else { //返回没有分类后的产品总数, total = entity.Product.Count(p => p.ProIsSell == false); lastPage = total / count + 1; products = (from p in entity.Product orderby p.ProID descending where p.ProIsSell == false select p).Skip((start - 1) * count).Take(count).ToList(); total = entity.Product.Count(p => p.ProIsSell == false); lastPage = total / count + 1; } //对start做了最大边界处理 start = start > lastPage ? lastPage : start; List <Dictionary <String, Object> > proList = new List <Dictionary <String, Object> >(); //将获取的到Product转成Dictionary对象 foreach (Product p in products) { proList.Add(ProductObjectToJson.Convert(p, null)); } return(proList); } }
public ActionResult MyReleaseProduct() { //1. 先判断用户是否登录了 if (!IsLogined()) { return(Content("<script>alert('请登录');window.location.href='/Users/Login'</script>")); } //通过Session获取id,不能通过url地址来获取 string id = Session["UserId"].ToString(); //2.对id进行合法性验证 int userid; if (!int.TryParse(id, out userid)) { return(Content("<script>alert('参数不合法');window.location.href='/Users/Login'</script>")); } //3. 获取该用户的发布产品的数据 /* * 遇到的bug:Entity Framework对象序列化出错:检测到循环引用 * 原因: 错误是EF的导航属性导致的,Product对象的ProWhoUser属性引用了Product对象导致无限循环,EF下很多问题ToList后通常能解决,但这次不行: * * 解决: 方法一:关闭导航功能(不能再使用导航属性): dc.Configuration.ProxyCreationEnabled = false; * 方法二:转为匿名对象:dc.Product.Where(p => p.ProWhoUser == userid).ToList(); */ using (ShoppingEntities dc = new ShoppingEntities()) { //手动将Product对象转成Dictionary对象 List <Dictionary <String, Object> > proList = new List <Dictionary <String, Object> >(); var retList = dc.Product.Where(p => p.ProWhoUser == userid && p.ProIsSell == false).ToList(); foreach (Product p in retList) { proList.Add(ProductObjectToJson.Convert(p, null)); } //var dataList = dc.Product.Where(p => p.ProWhoUser == userid && p.ProIsSell == false).Select((p => // new { p.ProID, p.ProName, p.ProLevel, p.ProOldPrice, p.ProNewPrice, p.ProIntro, p.ProImgUrl, p.ProReleaseTime } // )).ToList(); Dictionary <String, Object> retJson = new Dictionary <String, Object>(); retJson.Add("total", proList.Count()); retJson.Add("products", proList); return(Json(retJson, JsonRequestBehavior.DenyGet)); } }
public JsonResult getDetail(String id) { //参数有效性判断 int pid = 0; if (!int.TryParse(id, out pid)) { var ret = new { code = 400, message = "参数错误" }; return(Json(ret, JsonRequestBehavior.AllowGet)); } using (ShoppingEntities dc = new ShoppingEntities()) { //通过id和是否已出售来判断 var product = dc.Product.FirstOrDefault(p => p.ProID == pid && p.ProIsSell == false); //用过产品的 id 来获取用户数据 Users user = null; if (product != null) { user = dc.Users.FirstOrDefault(u => u.UserID == product.ProWhoUser); } else { var ret = new { code = 400, message = "没有该产品" }; return(Json(ret, JsonRequestBehavior.AllowGet)); } //将product对象转成json格式(包含用户信息), 订单对象为null Dictionary <String, Object> jsonProduct = new Dictionary <String, Object>(); jsonProduct.Add("product", ProductObjectToJson.Convert(product, null)); //添加产品的用户 jsonProduct.Add("user", UserObjectToJson.Convert(user)); return(Json(jsonProduct, JsonRequestBehavior.AllowGet)); } }
public ActionResult MyFavoriteProduct() { //1. 先判断用户是否登录了 if (!IsLogined()) { return(Content("<script>alert('请登录');window.location.href='/Users/Login'</script>")); } //通过Session获取id,不能通过url地址来获取 string id = Session["UserId"].ToString(); //2.对id进行合法性验证 int userid; if (!int.TryParse(id, out userid)) { return(Content("<script>alert('参数不合法');window.location.href='/Users/Login'</script>")); } //查询用户的收藏,这里需要判断该产品是否出售了 using (ShoppingEntities dc = new ShoppingEntities()) { //用于返回给前端的集合 List <Dictionary <String, Object> > favList = new List <Dictionary <String, Object> >(); //查询用户收藏的集合 var retList = dc.Favorite.Where(fav => fav.FavUID == userid && !fav.Product.ProIsSell).ToList(); foreach (var ret in retList) { favList.Add(ProductObjectToJson.Convert(ret.Product, null)); } Dictionary <String, Object> retJson = new Dictionary <String, Object>(); retJson.Add("total", favList.Count()); retJson.Add("products", favList); return(Json(retJson, JsonRequestBehavior.DenyGet)); } }