예제 #1
0
        public ActionResult Create(Station station)
        {
            var errors = ModelState.Values.SelectMany(v => v.Errors);

            if (!User.IsInRole("Admin"))
            {
                station.OwnerId = User.Identity.GetUserId();
            }

            if (ModelState.IsValid)
            {
                if (ValidateStation.AlreadyExistsStation(station))
                {
                    ModelState.AddModelError(string.Empty, "A estação já existe na base de dados.");

                    StationViewModel viewModel = new StationViewModel(db);
                    viewModel.Name      = station.Name;
                    ViewBag.ListRegions = ListRegionsById.createListItems(db, station.RegionId);
                    return(View(viewModel));
                }

                db.Stations.Add(station);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(new StationViewModel(db)));
        }
예제 #2
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Station station = db.Stations.Find(id);

            if (station == null)
            {
                return(HttpNotFound());
            }
            if (station.OwnerId != User.Identity.GetUserId() && !User.IsInRole("Admin"))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
            }
            ViewBag.ListRegions = ListRegionsById.createListItems(db, station.RegionId);
            return(View(station));
        }
예제 #3
0
        public ActionResult Edit(Station station)
        {
            station.OwnerId = User.Identity.GetUserId();

            if (ModelState.IsValid)
            {
                if (ValidateStation.AlreadyExistsStation(station))
                {
                    ModelState.AddModelError(string.Empty, "A estação já existe na base de dados.");

                    ViewBag.ListRegions = ListRegionsById.createListItems(db, station.RegionId);
                    return(View(station));
                }

                db.Entry(station).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(station));
        }