在HttpPost之后更新模型

时间:2011-06-29 07:24:13

标签: c# .net asp.net asp.net-mvc entity-framework

我想通过图像更新数据库中的现有Product对象,但只有在创建新对象时,图像才能成功进入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();
        }

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

5 个答案:

答案 0 :(得分:4)

我注意到你读过“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();
    }

答案 1 :(得分:3)

这是各种错误。

您应该使用特定的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);
}

Here's a good post讨论了ViewModel模式。

答案 2 :(得分:0)

尝试这样做

context.Products.Attach(product);

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

答案 3 :(得分:0)

试试这个:

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

注意:我会更改您确定新产品或更新产品的方式。

答案 4 :(得分:0)

[HttpPost]

public RedirectToRouteResult Save(TestViewModel viewModel)

{

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

}

相关问题