I want to update existing Product objects in database by images, but image goes to DB successfully only when i create new objects.
I'm trying to update my object this way
[HttpPost]
public ActionResult Edit(Product product, HttpPostedFileBase image)
{
if开发者_如何学C (ModelState.IsValid)
{
if (image != null)
{
product.ImageMimeType = image.ContentType;
product.ImageData = new byte[image.ContentLength];
image.InputStream.Read(product.ImageData, 0, image.ContentLength);
}
if (product.ProductID != 0)
UpdateModel<Product>(repository.Products.FirstOrDefault(p => p.ProductID == product.ProductID));
repository.SaveProduct(product);
TempData["message"] = string.Format("{0} has been saved", product.Name);
return RedirectToAction("Index");
}
return View(product);
}
//repository.SaveProduct()
public void SaveProduct(Product product)
{
if (product.ProductID == 0)
{
context.Products.Add(product);
}
context.SaveChanges();
}
The View
@
Upload new image: input type="file" name="Image"
input type="submit" value="Save"
@Html.ActionLink("Cancel and return to List", "Index")
}
I noticed you were read the "Pro ASP.NET MVC 3 Framework" and meet this issue same to me.
The author had a error at here, the code should be(You must reference and using the System.Data.Entity namespace at first):
public void SaveProduct(Product product)
{
if (product.ProductID == 0)
{
context.Products.Add(product);
}
else
{
context.Entry(product).State = System.Data.EntityState.Modified;
}
context.SaveChanges();
}
This is all kinds of wrong.
You should be using specific ViewModels for your Edit and Create actions.
Define a separate class containing the properties you wish to edit and any UI validation:
public class EditProductViewModel {
[HiddenInput]
public int Id {get;set;}
[Required]
public string Name {get;set;}
[Required]
public string Description {get;set;}
public HttpPostedFileBase Image {get;set;}
}
Then change your action method like so:
[HttpPost]
public ActionResult Edit(EditProductViewModel viewModel) {
if (ModelState.IsValid) {
var product = repository.Products.FirstOrDefault(p => p.Id == viewModel.Id);
// TODO - null check of product
// now lefty righty
product.Name = viewModel.Name;
product.Description = viewModel.Description;
if (viewModel.Image.ContentLength > 0) {
product.ImageMimeType = image.ContentType; // wouldn't trust this (better to lookup based on file extension)
product.ImageData = new byte[image.ContentLength];
image.InputStream.Read(product.ImageData, 0, image.ContentLength);
}
repository.SaveProduct(product);
return RedirectToAction("Index");
}
return View(viewModel);
}
Here's a good post discussing the ViewModel pattern.
Try doing this
context.Products.Attach(product);
Note: only when doing the update, not when inserting a new product.
Try this:
public void SaveProduct(Product product)
{
if (product.ProductID == 0)
{
context.Products.Add(product);
}
else // Update operation
{
context.Products.Attach(product);
}
context.SaveChanges();
}
Note: I would change the way you determine which is it a new or an updated product.
[HttpPost]
public RedirectToRouteResult Save(TestViewModel viewModel)
{
TempData["Output"] = "Here is some response";
return RedirectToAction("Index", viewModel);
}
精彩评论