MVC - 如果图像未在HTTP文档中重新上传(从SportsStore示例),请勿保存空图像数据 [英] MVC - Don't save null image data if image was not re-uploaded in HTTP post (from SportsStore example)

查看:127
本文介绍了MVC - 如果图像未在HTTP文档中重新上传(从SportsStore示例),请勿保存空图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Apress Pro ASP.NET MVC 3 Framework书中的SportsStore示例项目,并尝试将这些概念应用于我的应用程序。在这个示例中,我可以在示例中添加一个图像到产品,并将其保存到数据库中,但如果我编辑任何给定的产品,而不上传新的图像,图像数据将被清除。我想要编辑一个产品,但是如果从HTTP文件返回的图像数据为空,那么我希望Entity Framework保留现有的图像数据(和内容类型)。如果没有上传新的图像,我如何命令EF不更新此图像字段?

I have been following the SportsStore example project in Apress Pro ASP.NET MVC 3 Framework book and trying to apply the concepts to my application. One area that is bugging me is that in the sample, I can add an image to a product and it gets saved to the database, but if I edit any given product, without uploading a new image for it, the image data is cleared out. I want to be able to edit a product, but if the image data returned from the HTTP post is null, that I want Entity Framework to keep the exisiting image data (and content type). How can I command EF to not update this image field with null if a new image isn't uploaded?

以下是SportsStore示例中的编辑代码:

Here is the Edit code from the SportsStore sample:

[HttpPost]
public ActionResult Edit(Product product, HttpPostedFileBase image)
{
  if (ModelState.IsValid)
  {
    if(image != null)
    {
      product.ImageMimeType = image.ContentType;
      product.ImageData = new byte[image.ContentLength];
      image.InputStream.Read(product.ImageData, 0, image.ContentLength);
    }
    _repository.SaveProduct(product);
    TempData["message"] = string.Format("{0} has been saved", product.Name);
    return RedirectToAction("Index");
  }
  else
  {
    return View(product);
  }
}

编辑:对于Rondel - 产品类的定义

namespace SportsStore.Domain.Entities
{
  public class Product
  {
    [HiddenInput(DisplayValue=false)]
    public int ProductId { get; set; }

    [Required(ErrorMessage = "Please enter a product name")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Please enter a description")]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [Required]
    [Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
    public decimal Price { get; set; }

    [Required(ErrorMessage = "Please specify a category")]
    public string Category { get; set; }

    public byte[] ImageData { get; set; }

    [HiddenInput(DisplayValue = false)]
    public string ImageMimeType { get; set; }
  }
}

编辑我使EF仅保存某些字段,并将其他字段保留在数据库中?

EDIT How can I make EF save only certain fields and leave others untouched in the database?

推荐答案

这里的基本问题是,当您保存产品返回数据库,您将覆盖 ImageMimeType ImageData 字段与MVC3填充产品与$ code> FormCollection 的任何值。现在你有一个检查,看看 image == null ,但是你没有实现逻辑来重用旧的产品图像信息。这是你想要做的:

The basic problem here is that when you save the Product back to the database, you are overwriting the ImageMimeType and ImageData fields with whatever values MVC3 populates that Product with from the FormCollection. Right now you have a check to see if the image==null but you did not implement the logic to reuse the old Product image information. Here is what you want to do:

if (ModelState.IsValid)
{
  if(image != null)
  {
     product.ImageMimeType = image.ContentType;
     product.ImageData = new byte[image.ContentLength];
     image.InputStream.Read(product.ImageData, 0, image.ContentLength);
  }
 else
 {
    //set this Product image details from the existing product in the db
    product.ImageMimeType= getImageMimeTypeForProduct(product.ProductId );
    product.ImageData = getImageDataForProduct(product.ProductId );
 }
  _repository.SaveProduct(product);
  TempData["message"] = string.Format("{0} has been saved", product.Name);
  return RedirectToAction("Index");
}
else
{
  return View(product);
}

显然,这两种方法并不存在,但想法是一样的。您希望从该产品的数据库获取现有值,并确保在保存并覆盖该值之前,将它们反映在本地版本的 Product 中。

Obviously those two methods don't really exist but the idea is the same. You want to get the existing values from the db for that Product and ensure that those are reflected in the local version of the Product before you save it and overwrite the values.

这篇关于MVC - 如果图像未在HTTP文档中重新上传(从SportsStore示例),请勿保存空图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆