Saving Viewmodel data to the Database in ASP.NET MVC

asp.net-mvc asp.net-mvc-3 automapper entity-framework

Question

I am new to ASP.net MVC and am using a viewmodel rather than viewbags to populate my dropdowns since I've seen most people recommend against them. I have a slick UI that does cascading dropdowns and autocompletes (not shown here) but I can't seem to get my data saved 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 a few recommendations to use Automapper because EntityState.Modified won't work, but I'm not sure how to configure it because using the code below didn't work.

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

Any ideas?

1
8
4/30/2013 6:05:08 PM

Accepted Answer

Your view model should not be interacting with the database. View Models should only be used in the presentation layer (user interface) - hence the term "View" model. You should have another model (data model) that interacts with your database. Then you should have some type of service layer that handles your conversion between your view model and your data model (and vice versa). Your data model is the model generated by Entity Framework (which I assume is what you are using). To handle updates to your database, you need to instantiate a data context, grab the data entity from your database, make changes to that entity, and call save changes all in that data context. The data context will keep track of all changes to 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, context will keep track of any changes to "dataModel". When "context.SaveChanges" is called, those changes will automatically be applied to the database.

22
4/30/2013 7:07:39 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow