C# HttpPost之后的模型更新

C# HttpPost之后的模型更新,c#,.net,asp.net,asp.net-mvc,entity-framework,C#,.net,Asp.net,Asp.net Mvc,Entity Framework,我想通过映像更新数据库中现有的产品对象,但只有在创建新对象时,映像才能成功地转到DB 我正试图以这种方式更新我的对象 [HttpPost] public ActionResult Edit(Product product, HttpPostedFileBase image) { if (ModelState.IsValid) { if (image != null) { produc

我想通过映像更新数据库中现有的产品对象,但只有在创建新对象时,映像才能成功地转到DB

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

[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();
        }
[HttpPost]
公共操作结果编辑(产品,HttpPostedFileBase图像)
{
if(ModelState.IsValid)
{
如果(图像!=null)
{
product.ImageMimeType=image.ContentType;
product.ImageData=新字节[image.ContentLength];
image.InputStream.Read(product.ImageData,0,image.ContentLength);
}
if(product.ProductID!=0)
UpdateModel(repository.Products.FirstOrDefault(p=>p.ProductID==product.ProductID));
存储库。保存产品(产品);
TempData[“message”]=string.Format(“{0}已保存”,product.Name);
返回操作(“索引”);
}
返回视图(产品);
}
//repository.SaveProduct()
公共产品(产品)
{
if(product.ProductID==0)
{
context.Products.Add(产品);
}
SaveChanges();
}
景色

@
上载新图像:输入type=“file”name=“image”
输入type=“submit”value=“Save”
@ActionLink(“取消并返回列表”、“索引”)
}

尝试这样做

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]

公共重定向路由结果保存(TestViewModel viewModel)

{


}这是各种各样的错误

您应该使用特定的ViewModels进行编辑和创建操作

定义一个单独的类,其中包含要编辑的属性和任何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模式。

我注意到您阅读了“Pro ASP.NET MVC 3框架”,并遇到了与我相同的问题

作者在此处出错,代码应该是(您必须首先引用并使用System.Data.Entity命名空间):


为什么他应该改变他判断它是否是新对象的方式?我没有说“他应该”,但表示
EditMode
的布尔变量更健壮。为什么它更健壮?Id为0是很清楚的,不是吗?
[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);
}
    public void SaveProduct(Product product)
    {
        if (product.ProductID == 0)
        {
            context.Products.Add(product);
        }
        else
        {
            context.Entry(product).State = System.Data.EntityState.Modified;
        }

        context.SaveChanges();
    }