C# 使用EF&;更新数据库中的图像;MVC
我在数据库中上传和显示的图像可以正常工作,但当我尝试更新图像时,总是会出现错误。它给出一个错误:“对象引用未设置为对象的实例。” 代码如下:C# 使用EF&;更新数据库中的图像;MVC,c#,entity-framework,model-view-controller,C#,Entity Framework,Model View Controller,我在数据库中上传和显示的图像可以正常工作,但当我尝试更新图像时,总是会出现错误。它给出一个错误:“对象引用未设置为对象的实例。” 代码如下: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "StoreID,CustomerID,StoreName,StoreUID,StoreNumber,StoreLogo,StoreLogoPath,StoreAddress,St
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "StoreID,CustomerID,StoreName,StoreUID,StoreNumber,StoreLogo,StoreLogoPath,StoreAddress,StoreCity,StoreRegion,StoreCountry")] Store store, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
string ImageName = System.IO.Path.GetFileName(file.FileName);
string physicalPath = Server.MapPath("~/Images/" + ImageName);
file.SaveAs(physicalPath);
store.StoreLogoPath = Request.Form["StoreLogoPath"];
store.StoreLogo = ImageName;
db.Stores.Add(store);
db.SaveChanges();
}
return RedirectToAction("Index");
}
ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CustomerCompanyName", store.CustomerID);
return View(store);
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Store store = db.Stores.Find(id);
if (store == null)
{
return HttpNotFound();
}
ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CustomerCompanyName", store.CustomerID);
return View(store);
}
// POST: Stores/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "StoreID,CustomerID,StoreName,StoreUID,StoreNumber,StoreLogo,StoreLogoPath,StoreAddress,StoreCity,StoreRegion,StoreCountry")] Store store, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
string ImageName = System.IO.Path.GetFileName(file.FileName);
string physicalPath = Server.MapPath("~/Images/" + ImageName);
file.SaveAs(physicalPath);
store.StoreLogoPath = Request.Form["StoreLogoPath"];
store.StoreLogo = ImageName;
db.Entry(store).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CustomerCompanyName", store.CustomerID);
return View(store);
}
编辑视图
@使用(Html.BeginForm(新的{enctype=“multipart/form data”}))
{
商店标志
@Html.ValidationMessageFor(model=>model.StoreLogo,“,new{@class=“text danger”})
}
从何处获得该错误?在编辑代码的这一行-字符串ImageName=System.IO.Path.GetFileName(file.FileName);FileName
的值是多少?它只是检索上载图像的实际文件名。请调试并找出保存在FileName
中的特定值。我想,文件
设置不正确