Apress Pro ASP.NET MVC 3 Framework 서적에서 SportsStore 예제 프로젝트를 수행하고 내 응용 프로그램에 개념. 저를 괴롭히는 한 가지 예는 샘플에서 이미지를 제품에 추가 할 수 있고 데이터베이스에 저장되지만, 주어진 제품을 편집하면 새 이미지를 업로드하지 않고 이미지 데이터가 지워집니다 . 제품을 편집 할 수 있기를 원하지만 HTTP 게시물에서 반환 된 이미지 데이터가 null 인 경우 엔 Entity Framework에서 기존 이미지 데이터 (및 컨텐트 유형)를 유지해야합니다. 새 이미지를 업로드하지 않은 경우 EF가이 이미지 필드를 null로 업데이트하지 않도록하려면 어떻게해야합니까? 여기MVC - 이미지가 HTTP Post (SportsStore 예제에서)에 다시 업로드되지 않은 경우 null 이미지 데이터를 저장하지 마십시오.
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; }
}
}
편집는 어떻게 만들 수있는 제품 클래스의 정의입니다 - 론델의 경우 :
[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);
}
}
편집 : 여기
는 SportsStore 샘플에서 편집 코드 EF는 특정 필드 만 저장하고 다른 필드는 데이터베이스에 그대로 둡니다.
이미지 세부 정보를 담고있는 'Product' 클래스의 속성을 게시 할 수 있습니까? ImageMimeType 및 ImageData 필드를 사용하고 계십니까? – Rondel
Rondel - 위의 제품 클래스를 추가했습니다. – bigmac