public ActionResult ViewAutomobileDetails(int id) { var automobileInDB = _context.Automobiles.SingleOrDefault(c => c.Id == id); if (automobileInDB.Counter != null && User.IsInRole(RoleName.CanManageAutomobiles)) { automobileInDB.Counter = automobileInDB.Counter.Value + 1; } else if (User.IsInRole(RoleName.CanManageAutomobiles)) { automobileInDB.Counter = 1; } _context.SaveChanges(); var automobiles = _context.Automobiles.Where(c => c.Id == id).Include(c => c.Country).Include(c => c.AutomobileMake).Include(c => c.AutomobileModel).Include(c => c.Engine); var automobile = automobiles.SingleOrDefault(c => c.Id == id); //increasing the counter/number of views of the current automobile if (automobile == null) { return(HttpNotFound()); } IEnumerable <AutomobileDetail> automobileDetailsInDb = _context.AutomobileDetails.ToList().Where(c => c.AutomobileId == id); var detailsInDb = _context.Details.ToList(); List <AutomobileDetail> automobileDetails = new List <AutomobileDetail>(); foreach (var automobileDetail in automobileDetailsInDb) { automobileDetails.Add(automobileDetail); } var images = _context.FileModels.Where(m => m.AutomobileId == id).ToList(); var viewModel = new RandomAutomobileViewModel() { Automobile = automobile, FileModel = images, Details = detailsInDb, AutomobileDetails = automobileDetails }; if (detailsInDb.Count() > automobileDetailsInDb.Count()) { int dif = detailsInDb.Count() - automobileDetailsInDb.Count(); int i = 1; while (i <= dif) { viewModel.AutomobileDetails.Add(new AutomobileDetail()); i++; } } return(View("~/Views/Automobiles/ViewAutomobile.cshtml", viewModel)); }
// GET: Automobiles/Random public ActionResult Random() { var automobile = new Automobile() { Name = "Audi" }; var customers = new List <Customer> { new Customer { Name = "Customer 1" }, new Customer { Name = "Customer 2" } }; var viewModel = new RandomAutomobileViewModel { Automobile = automobile, Customers = customers }; return(View(viewModel)); }