Calling to UpdateModel in a controller is follow MVC pattern?? How could I do the same in the model layer?
newbie writing, I'm new to MVC 2 C# and using EntityFramework with LINQ.
In many examples (including the MVC 2 MusicStore of Microsoft) I've noticed that the Database updates are made in the Controllers layers. For example If I want to update a Database Product I call to UpdateModel(product,"Products" ) and call to SaveChanges() in the ProductController class
Ok, simple and easy but is this following MVC "traditional" pattern ??
Instead of this, I try to delegate the responsibility to the ProductModel class (I've made it with success in Adding and Deleting with AutoMapper) but I don't find the way to update from the model I tried something like
Mapper.CreateMap<Product,开发者_JAVA百科 ProductModels>();
Mapper.CreateMap<ProductModels, Product>();
var p = Mapper.Map<Product, ProductModels>(prod);
productosBD.AttachTo("Products", p);
productosBD.SaveChanges();
but no way...
Could somebody tell me how can I update an entity in the model layer??
Thanks in advance
ASP NET MVC has evolved a lot from version 1 Beta to version 2 and now version 3 although 2 and 3 are much more similar.
A lot of such samples are related to MVC 1 - something I learnt the hard way. I am pretty new to ASP NET MVC but realised in MVC 2, ModelBinder looks after updating the model so normally if you define your method signature as your model, you do not need to call UpdateModel
.
For example:
public ActionResult Create(MyModel model)
{
... // no need to call UpdateModel
However if you define it like this, then you need to do that:
public ActionResult Create(int id, FormCollection collection)
{
MyModel model = Repository.Get(id); // in your case EF productosBD.Where(x=>x.Id = id)
UpdateMode(model, collection);
Repository.Save(model);
In terms of where the update happens, yes, it will happen in your controller but it depends if you use a Repository pattern or use the bare database context. I personally use a ServiceProxy
which then talks to a WCF Service which talks to a Manager
which uses a Repository
to do that.
精彩评论