public JsonResult Inserting(string cpmc, int cplx, int cpdw, string cpjg, string cpms, string cpxh) { using (MainDb db = new MainDb()) { HttpFileCollectionBase files = Request.Files; string upload = ""; string fileName = ""; if (files.Count > 0)// { HttpPostedFileBase file = files[0]; if (file.FileName != null && !"".Equals(file.FileName))//表示有文件 { try { string Name = file.FileName; string[] NameArray = Name.Split(new char[] { '.' }); fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + NameArray[NameArray.Length - 1]; file.SaveAs(Server.MapPath("/Content/restaurant/img/upload") + "/" + fileName); upload = "success"; } catch (IOException e) { Console.WriteLine("IOException source: {0}", e.Source); upload = "fail"; } } } zzdc_cpxx cpxx = new zzdc_cpxx(); cpxx.name = cpmc; if (upload.Equals("success")) { cpxx.img = "/Content/restaurant/img/upload/" + fileName; } else { cpxx.img = "/Content/restaurant/img/miss.jpg"; } cpxx.cplx_id = cplx; cpxx.zd_id = cpdw; cpxx.price = Convert.ToSingle(cpjg); cpxx.sfsj = 0; cpxx.remark = cpms; cpxx.rank = Convert.ToInt32(cpxh); db.zzdc_cpxx.Add(cpxx); int count = db.SaveChanges(); if (count > 0) { return(Json(new { result = "success", upload = upload })); } else { return(Json(new { result = "fail", upload = upload })); } } }
public JsonResult Updating(int cpid, string cpmc, int cplx, int cpdw, string cpjg, string cpms, string cpxh) { using (MainDb db = new MainDb()) { HttpFileCollectionBase files = Request.Files; string upload = ""; string fileName = ""; if (files.Count > 0) { HttpPostedFileBase file = files[0]; if (file.FileName != null && !"".Equals(file.FileName))//表示有文件 { try { zzdc_cpxx cpxx = db.zzdc_cpxx.Where(p => p.id == cpid).FirstOrDefault(); if ("/Content/restaurant/img/miss.jpg".Equals(cpxx.img))//若图片miss { string Name = file.FileName; string[] NameArray = Name.Split(new char[] { '.' }); fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + NameArray[NameArray.Length - 1]; } else//置换图片 { string[] NameArray = cpxx.img.Split(new char[] { '/' }); fileName = NameArray[NameArray.Length - 1]; } file.SaveAs(Server.MapPath("/Content/restaurant/img/upload") + "/" + fileName); upload = "success"; } catch (IOException e) { Console.WriteLine("IOException source: {0}", e.Source); upload = "fail"; } } } string sql; if (upload.Equals("success")) { sql = "update zzdc_cpxx set img = '/Content/restaurant/img/upload/" + fileName + "'," + " name = '" + cpms + "'," + " cplx_id = " + cplx + "," + " zd_id = " + cpdw + "," + " price = " + cpjg + "," + " remark = '" + cpms + "'," + " rank = " + cpxh + " where id = " + cpid; } else { sql = "update zzdc_cpxx set name = '" + cpmc + "'," + " cplx_id = " + cplx + "," + " zd_id = " + cpdw + "," + " price = " + cpjg + "," + " remark = '" + cpms + "'," + " rank = " + cpxh + " where id = " + cpid; } int count = db.Database.ExecuteSqlCommand(sql); if (count > 0) { return(Json(new { result = "success" })); } else { return(Json(new { result = "fail" })); } } }