コード例 #1
0
ファイル: GoodsController.cs プロジェクト: wdy1/XueHai
        //[ValidateAntiForgeryToken]特性用来防止伪造的跨站请求,配合表单中的@Html.AntiForgeryToken()使用
        //对数据进行增删改时要防止csrf攻击!
        //该特性表示检测服务器请求是否被篡改。注意:该特性只能用于post请求,get请求无效。
        //public ActionResult GoodsCreate([Bind(Include = "Goods_id,GoodsName,GoodsImage,GoodsJianjie,GoodsDetails,AddTime,Price,Count,GoodsK_id")]Goods goods)
        public ActionResult GoodsCreate([Bind(Include = "Goods_id,GoodsName,GoodsImage,GoodsJianjie,GoodsDetails,AddTime,Price,Count,GoodsK_id")] Goods goods)
        {
            try
            {
                HttpPostedFileBase postimage1 = Request.Files["GoodsImage"];

                string filePath     = postimage1.FileName;
                string filename     = filePath.Substring(filePath.LastIndexOf("\\") + 1);
                string serverpath   = Server.MapPath(@"\Images\") + filename;
                string relativepath = @"/Images/" + filename;
                postimage1.SaveAs(serverpath);
                goods.GoodsImage = relativepath;

                goods.AddTime = System.DateTime.Now;
                goodsmanager.AddGoods(goods);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(Content(ex.Message));
            }
            ViewBag.GoodsK_id = new SelectList(db.GoodsK, "GoodsK_id", "GoodsKName", goods.GoodsK_id);
            return(View("GoodsCreate", goods));
        }
コード例 #2
0
 public ActionResult CreateGood(CreateGoodViewModel model)
 {
     if (ModelState.IsValid)
     {
         IBLL.IGoodsManager goodsManager = new GoodsManager();
         goodsManager.AddGoods(model.Name, model.ImgsUrl, model.Price, model.PriceOld);
         return(RedirectToAction("GoodsList"));
     }
     ModelState.AddModelError("", "您录入的信息有误");
     return(View(model));
 }
コード例 #3
0
        public ActionResult AddGoods(Good goods)
        {
            //上传图片
            HttpPostedFileBase image     = Request.Files["image"];
            string             SavePath  = Server.MapPath("~/Content/img/imageGood/");
            string             imageName = DateTime.Now.ToFileTime().ToString() + image.FileName; //获取图片名

            image.SaveAs(Path.Combine(SavePath, imageName));                                      //储存图片到物理路径

            goods.GoodPhoto = "/Content/img/imageGood/" + imageName;
            goods.ShopID    = Convert.ToInt32(Session["ShopID"]);
            GoodsManager goodsManager = new GoodsManager();

            goodsManager.AddGoods(goods);
            return(RedirectToAction("Index", "Home"));
        }