MVC - 如果图像未在 HTTP post 中重新上传,则不保存空图像数据(来自 SportsStore 示例)
我一直在关注 Apress Pro ASP.NET MVC 3 Framework 书中的 SportsStore 示例项目,并尝试将这些概念应用到我的应用程序中。困扰我的一个地方是,在示例中,我可以将图像添加到产品中并将其保存到数据库中,但是如果我编辑任何给定产品,而不为其上传新图像,则图像数据将被清除。我希望能够编辑产品,但如果从 HTTP post 返回的图像数据为空,我希望实体框架保留现有的图像数据(和内容类型)。如果未上传新图像,如何命令 EF 不将此图像字段更新为 null?
以下是 SportsStore 示例中的编辑代码:
[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 仅保存某些字段并保留其他字段数据库中未受影响?
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?
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);
}
}
EDIT: For Rondel - Here is the definition of the Product class
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; }
}
}
EDIT How can I make EF save only certain fields and leave others untouched in the database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的基本问题是,当您将
Product
保存回数据库时,您将使用 MVC3 填充的任何值覆盖ImageMimeType
和ImageData
字段来自FormCollection
的Product
。现在,您需要检查image==null
是否存在,但您没有实现重用旧Product
图像信息的逻辑。这就是您想要做的:显然这两种方法并不真正存在,但想法是相同的。您希望从数据库中获取该产品的现有值,并确保这些值反映在
Product
的本地版本中,然后再保存并覆盖这些值。The basic problem here is that when you save the
Product
back to the database, you are overwriting theImageMimeType
andImageData
fields with whatever values MVC3 populates thatProduct
with from theFormCollection
. Right now you have a check to see if theimage==null
but you did not implement the logic to reuse the oldProduct
image information. Here is what you want to do: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.我知道回复有点晚了,但我现在刚刚读完这一章,遇到了同样的问题。
我通过在编辑视图中添加一个隐藏字段来保存 ImageData 数据来修复它。由于视图使用 @Html.EditorForModel,因此不会为字节数据类型呈现任何编辑器,因此视图对此数据不可见。
I know its a bit late in replying but I'm just working through this chapter now and had the same issue.
I fixed it by adding a hidden field to the edit view to hold the ImageData data. As the view uses @Html.EditorForModel this does not render any editor for a byte data type, therefore the view has no visibility of this data.