コード例 #1
0
ファイル: BilController.cs プロジェクト: aspcodenet/SPOBilEF
        public ActionResult View(int id)
        {
            using (var db = new Models.BilModel2())
            {
                var bil   = db.Bilar.FirstOrDefault(r => r.Id == id);
                var model = new ViewModels.BilViewViewModel
                {
                    Color        = bil.Color,
                    Manufacturer = bil.Manufacturer,
                    Model        = bil.Model,
                    Price        = bil.Price,
                    Year         = bil.Year
                };

                return(View(model));
            }
        }
コード例 #2
0
ファイル: BilController.cs プロジェクト: aspcodenet/SPOBilEF
 public ActionResult Edit(int id)
 {
     using (var db = new Models.BilModel2())
     {
         var bil   = db.Bilar.FirstOrDefault(p => p.Id == id);
         var model = new ViewModels.BilEditViewModel
         {
             Color          = bil.Color,
             Manufacturer   = bil.Manufacturer,
             Modell         = bil.Model,
             NumberOfWheels = bil.NumberOfWheels,
             //Price = bil.Price,
             Year = bil.Year,
             Id   = bil.Id
         };
         return(View(model));
     }
 }
コード例 #3
0
ファイル: BilController.cs プロジェクト: aspcodenet/SPOBilEF
 public ActionResult Create(ViewModels.BilCreateViewModel model)
 {
     if (!ModelState.IsValid)
     {
         return(View(model));
     }
     using (var db = new Models.BilModel2())
     {
         var bil = new Models.Bil
         {
             Color        = model.Color,
             Manufacturer = model.Manufacturer,
             Model        = model.Modell,
             Year         = model.Year
         };
         db.Bilar.Add(bil);
         db.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
コード例 #4
0
ファイル: BilController.cs プロジェクト: aspcodenet/SPOBilEF
        public ActionResult Edit(ViewModels.BilEditViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            using (var db = new Models.BilModel2())
            {
                var bil = db.Bilar.FirstOrDefault(r => r.Id == model.Id);
                bil.Manufacturer = model.Manufacturer;
                bil.Model        = model.Modell;
                //bil.Price = model.Price;
                bil.Year           = model.Year;
                bil.NumberOfWheels = model.NumberOfWheels;
                bil.Color          = model.Color;
                db.SaveChanges();
            }


            return(RedirectToAction("Index"));
        }
コード例 #5
0
ファイル: BilController.cs プロジェクト: aspcodenet/SPOBilEF
        public ActionResult Search(string SearchManufacturer, string SearchYear)
        {
            using (var db = new Models.BilModel2())
            {
                var model = new ViewModels.BilIndexViewModel
                {
                    SearchManufacturer = SearchManufacturer,
                    SearchYear         = SearchYear
                };
                model.Cars.AddRange(db.Bilar.ToList().Select(r => new ViewModels.BilIndexViewModel.BilListViewModel
                {
                    Manufacturer = r.Manufacturer,
                    Model        = r.Model,
                    Year         = r.Year,
                    Id           = r.Id
                }).Where(c => Matches(c, SearchManufacturer, SearchYear)
                         ));

                return(View("Index", model));
            }
        }
コード例 #6
0
ファイル: BilController.cs プロジェクト: aspcodenet/SPOBilEF
        // GET: Bil
        public ActionResult Index(string sort)
        {
            var model = new ViewModels.BilIndexViewModel();

            using (var db = new Models.BilModel2())
            {
                //var carsFromDB = db.Bilar.AsQueryable();
                //if (sort == "NamnAsc")
                //    carsFromDB = carsFromDB.OrderBy(r => r.Manufacturer);
                //else if (sort == "NamnDesc")
                //    carsFromDB = carsFromDB.OrderByDescending(r => r.Manufacturer);


                //if (sort == "YearAsc")
                //    carsFromDB = carsFromDB.OrderBy(r => r.Year);
                //else if (sort == "YearDesc")
                //    carsFromDB = carsFromDB.OrderByDescending(r => r.Year);

                //model.Cars.AddRange(carsFromDB.Select( r => new ViewModels.BilIndexViewModel.BilListViewModel
                //{
                //    Manufacturer = r.Manufacturer,
                //    Model = r.Model,
                //    Year = r.Year,
                //    Id = r.Id
                //}));



                model.Cars.AddRange(db.Bilar.Select(r => new ViewModels.BilIndexViewModel.BilListViewModel
                {
                    Manufacturer = r.Manufacturer,
                    Model        = r.Model,
                    Year         = r.Year,
                    Id           = r.Id
                }));

                if (sort == "NamnAsc")
                {
                    model.Cars = model.Cars.OrderBy(r => r.Manufacturer).ToList();
                }
                else if (sort == "NamnDesc")
                {
                    model.Cars = model.Cars.OrderByDescending(r => r.Manufacturer).ToList();
                }


                if (sort == "YearAsc")
                {
                    model.Cars = model.Cars.OrderBy(r => r.Year).ToList();
                }
                else if (sort == "YearDesc")
                {
                    model.Cars = model.Cars.OrderByDescending(r => r.Year).ToList();
                }


                model.CurrentSort = sort;

                return(View(model));
            }
        }