.net - Why the SaveChanges() can't work when I try to finish the SportStore Demo in the book of Pro ASP.NET MVC3? -


public void saveproduct(product product) {     if (product.productid == 0)          {             context.products.add(product);          }         //oops~~~      context.savechanges(); }  [httppost] public actionresult edit(product product) {     if (modelstate.isvalid)     {         repository.saveproduct(product);         //i can see msg int view page. database never changed.!!         tempdata["message"] = string.format("{0} has been saved", product.name);         return redirecttoaction("index");     }     else     {         // there wrong data values         return view(product);     } } 

i got stuck problem , don't know how make data stored database. problem occurs when try save changes existing product. can tell me why savechanges() method called , datas never saved db? thx

the entity product constructed model binding not automatically attached context. context therefore not aware of change save. have attach product first , set it's state modified.

context.products.attach(product); // when setting entry state modified // properties of entity marked modified. context.entry(product).state = entitystate.modified; 

now can call context.savechanges();.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -