Apress Pro Asp.net MVC Framework 3 - SportsStore Edit Product not working?

浪尽此生 提交于 2019-12-03 11:11:40

The state of the EF object needs to be updated before saving.

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


            int result = context.SaveChanges();

        }

I can't yet post comments but I would like to add to MVC Newbie's comment by showing what your final method should read:

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

Also don't forget to add the extra using statement (again as mentioned by MVC Newbie):

//using system.data;
DatH

Try the followings. The idea is that product param that MVC model binding to the Action method is not syn'ing with EF, so we need to assocaite it to the repository:

public ActionResult Edit(Product product)   
{
    if (ModelState.IsValid)
    {
        ((ObjectSet<Product>)repository.Products).ApplyCurrentValues(product);

        repository.SaveProduct(product);
        TempData["message"] = string.Format("{0} has been saved", product.Name);
        return RedirectToAction("Index");
    }
    else 
    {
        return View(product);
    }
}

I came across the same issue using the final version of Apress Pro ASP.NET MVC3. Using the Visual Studio debugger I noticed that when the context.SaveChanges()(SportsStore.Domain.Concrete.EFProductRepoistory) was executed the context was not changed to the changes we made inside the Edit view. Though the product defined in the constructor of SaveProduct()

So i guessed all we had to do is change the Context.Products.Product to the product inside the constructor like this:

        else
        {
            context.Products.Find(product.ProductId) = product;
        }

unfortunately Visual Studio gave me this error:

Error 1 The left-hand side of an assignment must be a variable, property or indexer

So to make it work I had to do this:

        else
        {
            context.Products.Find(product.ProductID).Name = product.Name;
            context.Products.Find(product.ProductID).Description = product.Description;
            context.Products.Find(product.ProductID).Category = product.Category;
            context.Products.Find(product.ProductID).Price = product.Price;
        }

This does work. However I think this is far from ideal and not the best way to do this.

Is there a way to do this in a way where I just edit/update the whole Product object inside the context rather than edit every property one by one?

maraikayer
public void SaveProduct(Product product)
{
    var prod = context.Products.SingleOrDefault(p => p.ProductID == product.ProductID);
    if (product.ProductID > 0)  
    {
        context.Products.Remove(prod);
    }
    context.Products.Add(product);            
    context.SaveChanges();
}

try this

 public ActionResult Edit(Product product)
    {
        if (ModelState.IsValid)
        {
            Product p = repository.Products.FirstOrDefault(x => x.ProductID == product.ProductID);
            if (p != null)
            {
                p.ProductID = product.ProductID;
                p.Price = product.Price;
                p.Category = product.Category;
                p.Description = product.Description;
                p.Name = product.Name;
            }
            else
                p = product;
            repository.SaveProduct(p);
            TempData["message"] = string.Format("{0} has been saved", product.Name);
            return RedirectToAction("Index");
        }
        else 
        {
            return View(product);
        }
    }

im beginner with mvc 3, but i think the model reference (product parameter) is a Product object that is not bind to EF Context

maraikayer

Here is the answer

public void SaveProduct(Product product)
    {
        var prod = context.Products.SingleOrDefault(p => p.ProductID == product.ProductID);

        if (product.ProductID > 0)
        {
            context.Products.Remove(prod);
        }
            context.Products.Add(product);            
            context.SaveChanges();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!