public async Task <IActionResult> Edit(int id, [Bind("Id,VehicleType,RegNumber,Color,Brand,Model,NumberOfWheels,ArrivalTime")] ParkedVehicleViewModel pVVM)
        {
            bool RegNumberExist = db.ParkedVehicle.Any(v => v.Id != pVVM.Id && v.RegNumber.ToLower().Equals(pVVM.RegNumber.ToLower()));
            //bool RegNumberExist2 = string.IsNullOrEmpty(pVVM.RegNumber) ? false : db.
            var pv = await db.ParkedVehicle.FindAsync(id);

            ParkedVehicleInstance(pVVM, pv);

            if (RegNumberExist)
            {
                ModelState.AddModelError(string.Empty, "Registration Number already exist.");
            }
            else if (ModelState.IsValid)
            {
                try
                {
                    db.Update(pv);
                    db.Entry(pv).Property("ArrivalTime").IsModified = false;
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ParkedVehicleExists(pv.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(pVVM));
        }
        // GET: ParkedVehicles2
        public async Task <IActionResult> Index(string VehicleType, string regNbr)
        {
            ParkedVehicleViewModel parkedVehicleViewModel = new ParkedVehicleViewModel();

            var mvcGarage2Context = await _context.ParkedVehicle.Include(p => p.VehicleType).Include(p => p.Member).ToListAsync();

            // Filtrera på fordonstyp
            if (!string.IsNullOrEmpty(VehicleType))
            {
                mvcGarage2Context = mvcGarage2Context
                                    .Where(v => v.VehicleType.Type == VehicleType)
                                    .ToList();
            }

            // Filtrera på registreringsnummer
            if (!string.IsNullOrEmpty(regNbr))
            {
                //regNbr = regNbr.Trim().ToUpper();
                //parkedVehicleViewModel.RegNbr = regNbr;
                mvcGarage2Context = mvcGarage2Context.Where(p => p.RegistrationNumber.Contains(regNbr.Trim().ToUpper())).ToList();
            }

            parkedVehicleViewModel.ParkedVehicle = mvcGarage2Context;

            // Lista fordonstyper
            IEnumerable <string> vehicleTypes = from v in _context.VehicleType
                                                orderby v.Type
                                                select v.Type;

            parkedVehicleViewModel.VehicleTypes = new SelectList(vehicleTypes.Distinct());

            return(View(parkedVehicleViewModel));
        }
        public async Task <IActionResult> Edit(int?id)
        {
            ParkedVehicle p = await db.ParkedVehicle.FindAsync(id);

            ParkedVehicleViewModel pVVM = ParkedVehicleViewModelInstance(p);

            return(View(pVVM));
        }
 private void ParkedVehicleInstance(ParkedVehicleViewModel pVVM, ParkedVehicle pv)
 {
     pv.VehicleType    = pVVM.VehicleType;
     pv.RegNumber      = pVVM.RegNumber;
     pv.Color          = pVVM.Color;
     pv.Brand          = pVVM.Brand;
     pv.Model          = pVVM.Model;
     pv.NumberOfWheels = pVVM.NumberOfWheels;
 }
Exemplo n.º 5
0
        // GET: ParkedVehicles/Create
        public ActionResult Create()
        {
            var model = new ParkedVehicleViewModel()
            {
                Members           = db.Members,
                ValidVehicleTypes = db.VehicleTypes
            };

            return(View(model));
        }
        // GET: ParkedVehicles/Create
        public ActionResult Create()
        {
            Functionalities.DoIt doIt = new Functionalities.DoIt();
            //ViewBag.MemberId = new SelectList(db.Members, "Id", "PersonNumber");
            //ViewBag.VehicleTypeId = new SelectList(db.VehicleTypes, "Id", "Name");

            ParkedVehicleViewModel pvvm = new ParkedVehicleViewModel();

            pvvm.Members      = new SelectList(db.Members.ToList(), "Id", "Firstname");
            pvvm.VehicleTypes = new SelectList(db.VehicleTypes.ToList(), "Id", "Name");

            return(View(pvvm));
        }
        private ParkedVehicleViewModel ParkedVehicleViewModelInstance(ParkedVehicle p)
        {
            var pVVM = new ParkedVehicleViewModel
            {
                Id             = p.Id,
                VehicleType    = p.VehicleType,
                RegNumber      = p.RegNumber,
                Color          = p.Color,
                Brand          = p.Brand,
                Model          = p.Model,
                NumberOfWheels = p.NumberOfWheels,
                ArrivalTime    = p.ArrivalTime,
                ParkedTime     = DateTime.Now - p.ArrivalTime
            };

            return(pVVM);
        }
        public ActionResult Create([Bind(Include = "MemberId,RegNumber,Color,VehicleTypeId,Brand,NumberOfWheels")] ParkedVehicleViewModel pvvm)
        {
            VehicleType   vt            = db.VehicleTypes.Single(v => v.Id == pvvm.VehicleTypeId);
            ParkedVehicle parkedVehicle = new ParkedVehicle(pvvm.Member, pvvm.RegNumber, pvvm.Color, vt, pvvm.Brand, pvvm.NumberOfWheels, DateTime.Now);

            parkedVehicle.Member      = db.Members.Single(m => m.Id == pvvm.MemberId);
            parkedVehicle.VehicleType = vt;

            if (ModelState.IsValid)
            {
                parkedVehicle.Size = parkedVehicle.VehicleType.Size;
                db.ParkedVehicles.Add(parkedVehicle);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            pvvm.Members      = new SelectList(db.Members.ToList(), "Id", "PersonNumber");
            pvvm.VehicleTypes = new SelectList(db.VehicleTypes.ToList(), "Id", "Name");
            return(View(pvvm));
        }
Exemplo n.º 9
0
        public ActionResult Create([Bind(Include = "Id,RegistrationNumber,Color,Brand,Model,VTId,NumberOfWheels,MemberID")] ParkedVehicleViewModel pVModel)
        {
            //DateTime CheckIn = DateTime.Now;

            var parkedVehicle = new ParkedVehicle()
            {
                ParkTime           = DateTime.Now,
                Brand              = pVModel.Brand,
                RegistrationNumber = pVModel.RegistrationNumber,
                Model              = pVModel.Model,
                NumberOfWheels     = pVModel.NumberOfWheels,
                Color              = pVModel.Color,
                MemberId           = pVModel.MemberId,
                VTId = pVModel.VTId
            };

            //db.Vehicles.Any(p => p.RegNumber == c);
            //var no = db.Vehicles.FirstOrDefault(w => w.Id == );
            //if (no == null)

            try
            {
                if (ModelState.IsValid)
                {
                    db.ParkedVehicles.Add(parkedVehicle);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }



            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.)
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }
            PopulateMembersDropDownList(parkedVehicle.MemberId);

            return(View(parkedVehicle));
        }