ActionResult使用MVC5编辑图像

时间:2018-02-04 12:32:15

标签: asp.net-mvc

我已成功保存并使用

显示图像

控制器

 public ActionResult Create(HttpPostedFileBase file)
    {
        if (file != null)
        {
            TestEntities db = new TestEntities();
            string ImageName = System.IO.Path.GetFileName(file.FileName);
            string physicalPath = Server.MapPath("~/images/" + ImageName);


            file.SaveAs(physicalPath);


            Client newRecord = new Client();
            newRecord.TitelAr = Request.Form["TitelAr"];
            newRecord.descriptionAr = Request.Form["descriptionAr"];
            //newRecord.DateTime = Request.Form["DateTime"];
            newRecord.Img = ImageName;
            db.Client.Add(newRecord);
            db.SaveChanges();

        }


        return RedirectToAction("../Clients/Index/");
    }

但我无法使用编辑视图更改图像,以下代码保存相同的图像

控制器

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Client client, HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {
            var productInDb = db.Client.Find(client.ID);
            productInDb.TitelAr = client.TitelAr;
            productInDb.descriptionAr = client.descriptionAr;

            if (client.Img != null)
            {
                string ImageName = System.IO.Path.GetFileName(file.FileName);
                string physicalPath = Server.MapPath("~/images/" + ImageName);
                productInDb.Img = ImageName;
            }

            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(client);
    }

我需要更改它,请帮我改变编辑视图中的图像, 谢谢

1 个答案:

答案 0 :(得分:0)

你应该控制文件对象。

if (file != null)
            {
                string ImageName = System.IO.Path.GetFileName(file.FileName);
                string physicalPath = Server.MapPath("~/images/" + ImageName);
                productInDb.Img = ImageName;
            }