G’day All, has anyone purchased the ALPHA of Apress Pro Asp.net MVC Framework 3 and created the SportsStore? I can’t for the life of me edit products and have the DB update successfully? No errors are displayed and unit tests all function, but no successful ‘edit’ i.e. I change some details, click save, it reports successful – I then check the results, and nothing has happened? Did anyone else find this when working through the SportsStore? Any advice will be greatly appreciated.
Cheers.
Here is the answer
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:
The state of the EF object needs to be updated before saving.
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:
Also don't forget to add the extra using statement (again as mentioned by MVC Newbie):
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:
unfortunately Visual Studio gave me this error:
So to make it work I had to do this:
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?