在HttpPost之后更新模型

本文关键字:模型 更新 之后 HttpPost | 更新日期: 2023-09-27 18:00:34

我想通过图像更新数据库中现有的Product对象,但只有当我创建新对象时,图像才能成功地进入数据库。

我正在尝试以这种方式更新我的对象

[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);
            }
            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();
        }

视图 @ Upload new image: input type="file" name="Image" input type="submit" value="Save" @Html.ActionLink("Cancel and return to List", "Index") }

在HttpPost之后更新模型

我注意到你读过"Pro ASP.NET MVC 3 Framework",我也遇到过这个问题。

作者在这里有一个错误,代码应该是(首先必须引用并使用System.Data.Entity命名空间):

    public void SaveProduct(Product product)
    {
        if (product.ProductID == 0)
        {
            context.Products.Add(product);
        }
        else
        {
            context.Entry(product).State = System.Data.EntityState.Modified;
        }
        context.SaveChanges();
    }

这是各种各样的错误。

您应该为"编辑"answers"创建"操作使用特定的ViewModel。

定义一个单独的类,其中包含要编辑的属性和任何UI验证:

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;}
}

然后像这样改变你的行动方式:

[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);
}

这是一篇讨论ViewModel模式的好文章。

尝试执行此

context.Products.Attach(product);

注意:只有在进行更新时,而不是在插入新产品时。

试试这个:

public void SaveProduct(Product product)
{
    if (product.ProductID == 0)
    {
        context.Products.Add(product);
    }
    else    // Update operation
    {
        context.Products.Attach(product);
    }
    context.SaveChanges();
}

注意:我会改变你确定它是新产品还是更新产品的方式。

[HttpPost]

public RedirectToRouteResult Save(TestViewModel viewModel)

TempData["Output"] = "Here is some response";
return RedirectToAction("Index", viewModel);

}