Saving ViewModel data to a database in ASP.NET MVC

I am new to ASP.net MVC and use viewmodel rather than viewbags to populate my dropdowns, as I have seen most people recommend against them. I have a slick UI that does cascading drop-down lists and autocomplete (not shown here), but I cannot get my data back to the database.

Models:

public partial class Car { public int CarID { get; set; } public string CarName { get; set; } public int ModelID { get; set; } public int ManufacturerID { get; set; } public int CarColorID { get; set; } public Nullable<decimal> Price { get; set; } public string Description { get; set; } public virtual CarColor CarColor { get; set; } public virtual Manufacturer Manufacturer { get; set; } public virtual CarModel CarModel { get; set; } } public partial class CarColor { public CarColor() { this.Cars = new HashSet<Car>(); } public int ColorID { get; set; } public string ColorName { get; set; } public virtual ICollection<Car> Cars { get; set; } } public partial class CarModel { public CarModel() { this.Cars = new HashSet<Car>(); } public int CarModelID { get; set; } public int ManufacturerID { get; set; } public string CarModelName { get; set; } public virtual ICollection<Car> Cars { get; set; } public virtual Manufacturer Manufacturer { get; set; } } public partial class Manufacturer { public Manufacturer() { this.Cars = new HashSet<Car>(); this.Manufacturer1 = new HashSet<Manufacturer>(); this.CarModels = new HashSet<CarModel>(); } public int ManufacturerID { get; set; } public string ManufacturerName { get; set; } public Nullable<int> ParentID { get; set; } public virtual ICollection<Car> Cars { get; set; } public virtual ICollection<Manufacturer> Manufacturer1 { get; set; } public virtual Manufacturer Manufacturer2 { get; set; } public virtual ICollection<CarModel> CarModels { get; set; } } 

ViewModel:

 public class AnotherTestViewModel { public Car car { get; set; } public IEnumerable<SelectListItem> CarModels { get; set; } public IEnumerable<SelectListItem> Manufacturers { get; set; } public IEnumerable<SelectListItem> CarColors { get; set; } } 

Controller:

 public ActionResult Create() { var model = new AnotherTestViewModel(); using (new CarTestEntities()) { model.CarModels = db.CarModels.ToList().Select(x => new SelectListItem { Value = x.CarModelID.ToString(), Text = x.CarModelName }); model.Manufacturers = db.Manufacturers.ToList().Select(x => new SelectListItem { Value = x.ManufacturerID.ToString(), Text = x.ManufacturerName }); model.CarColors = db.CarColors.ToList().Select(x => new SelectListItem { Value = x.ColorID.ToString(), Text = x.ColorName }); } return View(model); } // // POST: /AnotherTest/Create [HttpPost] public ActionResult Create(AnotherTestViewModel model) { if (ModelState.IsValid) { db.Entry(model).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Details", "AnotherTestViewModel", new { id = model.car.CarID }); } return View(); } 

I saw several recommendations for using Automapper, because EntityState.Modified will not work, but I'm not sure how to configure it, because using the code below does not work.

 Mapper.CreateMap<AnotherTestViewModel, Car>(); Mapper.CreateMap<Car, AnotherTestViewModel>(); var newCar = Mapper.Map<AnotherTestViewModel, Car>(model); 

Any ideas?

+7
source share
1 answer

Your view model should not interact with the database. View models should only be used in the presentation layer (user interface) - hence the term “View”. You must have another model (data model) that interacts with your database. Then you should have some type of service level that handles your conversion between your view model and your data model (and vice versa). Your data model is a model created by the Entity Framework (which I assume you are using). In order to process updates in your database, you need to create an instance of the data context, grab the data object from your database, make changes to this object and cause the changes to be saved in the data context. The data context will track all changes in your entities and apply the necessary changes to your database when you call "save changes." Example:

 public void UpdateCar(CarViewModel viewModel) { using (DataContext context = new DataContext()) { CarEntity dataModel = context.CarEntities.where(x => x.Id == viewModel.Id).First(); dataModel.Name = viewModel.Name; dataModel.Type = viewModel.Type; context.SaveChanges(); } } 

In this example, the context will track any changes to the "dataModel". When "context.SaveChanges" is called, these changes will be automatically applied to the database.

+21
source

All Articles