コード例 #1
0
        public async Task <IActionResult> Create([Bind("Id,RegNr,NoWheels,Color,Brand,Model,TimeOfArrival")] ParkedVehicle parkedVehicle)
        {
            if (ModelState.IsValid)
            {
                _context.Add(parkedVehicle);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(parkedVehicle));
        }
コード例 #2
0
ファイル: MembersController.cs プロジェクト: mawall7/Garage_2
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,Phone,SocialSecurityNumber,Email,Street,ZIP,Avatar")] Member member)
        {
            if (member.Avatar == null)
            {
                member.Avatar = "https://secure.gravatar.com/avatar/831eb2fb911095cd15676650e5aed871?s=128&d=mm&r=g";
            }
            if (ModelState.IsValid)
            {
                db.Add(member);
                await db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(member));
        }
コード例 #3
0
        public async Task <IActionResult> Create
        (
            [Bind("Id,RegisterNumber,Color,Model,Brand,WheelsNumber,ParkedDateTime")] ParkedVehicle parkedVehicle,
            string Members,             // From View member dropdown select
            string VehicleTypes,        // From View vehicle type dropdown select
            string newVehicleType       // From new vehicle input text field
        )
        {
            DateTime now = DateTime.Now;

            parkedVehicle.ParkedDateTime = now;


            bool IsProductRegNumberExist = db.ParkedVehicle.Any  // logic for reg nr
                                               (x => x.RegisterNumber == parkedVehicle.RegisterNumber && x.Id != parkedVehicle.Id);

            if (IsProductRegNumberExist == true)
            {
                ModelState.AddModelError("RegisterNumber", "RegisterNumber already exists");
            }

            int parsedMemberId = 0;

            if (Int32.TryParse(Members, out parsedMemberId))
            {
                if (parsedMemberId == 0)
                {
                    // MemberId is used in Create.cshtml @Html.ValidationMessage(
                    ModelState.AddModelError("MemberId", "No member chosen!");
                }

                parkedVehicle.MemberId = parsedMemberId;
            }

            // If user adds new vehicle type, the new vehicle type overrides the vehicle type chosen from the dropdown select
            if (String.IsNullOrWhiteSpace(newVehicleType)) // No new vehicle type given by user
            {
                int parsedVehicleType = 0;

                if (Int32.TryParse(VehicleTypes, out parsedVehicleType))
                {
                    if (parsedVehicleType == 0)
                    {
                        // VehicTyp is used in Create.cshtml @Html.ValidationMessage(
                        ModelState.AddModelError("VehicTyp", "No vehicle type chosen!");
                    }

                    parkedVehicle.VehicleTypeId = parsedVehicleType;
                }
            }
            else
            {
                bool isVehicleTypeUnique = checkIfVehicleTypeIsUnique(newVehicleType);

                if (isVehicleTypeUnique == false)
                {
                    ModelState.AddModelError("VehicleTypeExist", "Vehicle type already exists in database");
                }

                // Create new Vehicle Type
                VehicleType newVT = new VehicleType();
                newVT.VehicType = newVehicleType;
                db.VehicleType.Add(newVT);
                await db.SaveChangesAsync();

                parkedVehicle.VehicleType = newVT;
            }

            if (ModelState.IsValid)
            {
                db.Add(parkedVehicle);
                await db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                CreateDropDownLists();
            }

            return(View(parkedVehicle));
        }