public ActionResult MyCargoes([Bind(Include = "Id,Emeil")] ModelWithIdAndEmeil model) { Forwarder forwarder = null; Carrier carrier = null; try { forwarder = db.Fowarders.Where(f => f.ForwarderIdentificator == model.Id && f.Emeil == model.Emeil).First(); } catch (Exception) { forwarder = null; } try { carrier = db.Carriers.Where(c => c.CarrierIdentificator == model.Id && c.Emeil == model.Emeil).First(); } catch (Exception) { carrier = null; } if (forwarder == null && carrier == null) { return(RedirectToAction("ErrorUser")); } List <Cargo> cargoes = new List <Cargo>(); if (forwarder != null) { cargoes = db.Cargoes.Where(c => c.ForwarderId == forwarder.ForwarderId).ToList(); } else { cargoes = db.Cargoes.Where(c => c.CarrierId == carrier.CarrierId).ToList(); } ModelStaticList.cargoModels = new List <CargoView>(); foreach (var cargo in cargoes) { CargoView cargoView = new CargoView(); cargoView.Id = cargo.CargoId; cargoView.Name = cargo.Name; cargoView.PricePerKilometer = cargo.PricePerKilometer; cargoView.Weight = cargo.Weight; Location fromLocation = db.Locations.Where(l => l.LocationId == cargo.FromLocationId).First(); Location forLocation = db.Locations.Where(l => l.LocationId == cargo.ForLocationId).First(); cargoView.FromLocation = fromLocation.Name; cargoView.ForLocation = forLocation.Name; cargoView.CargoType = cargo.CargoType.ToString(); cargoView.TransportType = cargo.TransportType.ToString(); Forwarder forwarderIn = db.Fowarders.Where(f => f.ForwarderId == cargo.ForwarderId).First(); cargoView.Forwarder = forwarderIn.Name; ModelStaticList.cargoModels.Add(cargoView); } return(RedirectToAction("AllMyCargoes")); }
public ActionResult AllCargoes() { List <Cargo> cargoes = db.Cargoes.Where(c => c.CarrierId < 1 || c.CarrierId == null).ToList(); List <CargoView> cargoViews = new List <CargoView>(); foreach (var cargo in cargoes) { CargoView cargoView = new CargoView(); cargoView.Id = cargo.CargoId; cargoView.Name = cargo.Name; cargoView.PricePerKilometer = cargo.PricePerKilometer; cargoView.Weight = cargo.Weight; Location fromLocation = db.Locations.Where(l => l.LocationId == cargo.FromLocationId).First(); Location forLocation = db.Locations.Where(l => l.LocationId == cargo.ForLocationId).First(); cargoView.FromLocation = fromLocation.Name; cargoView.ForLocation = forLocation.Name; Forwarder forwarder = db.Fowarders.Where(f => f.ForwarderId == cargo.ForwarderId).First(); cargoView.CargoType = cargo.CargoType.ToString(); cargoView.TransportType = cargo.TransportType.ToString(); cargoView.Forwarder = forwarder.Name; cargoViews.Add(cargoView); } return(View(cargoViews)); }
public ActionResult SearchCargo([Bind (Include = "Name,PricePerKilometer,Weight,FromLocation,ForLocation,CargoType,TransportType")] SearchModel model) { if (model == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Location fromLocation = null; Location forLocation = null; try { fromLocation = db.Locations.Where(l => l.Name == model.FromLocation).First(); } catch (Exception) { fromLocation = null; } try { forLocation = db.Locations.Where(l => l.Name == model.ForLocation).First(); } catch (Exception) { forLocation = null; } if (model.Name == null || model.Name == "") { model.Name = "empty"; } if (model.PricePerKilometer < 1 || model.PricePerKilometer == null) { model.PricePerKilometer = 0; } if (model.Weight == null || model.Weight < 1) { model.Weight = 0; } if (fromLocation == null) { fromLocation = new Location(); fromLocation.LocationId = 0; } if (forLocation == null) { forLocation = new Location(); forLocation.LocationId = 0; } if (model.TransportType == null || model.TransportType == "") { model.TransportType = "empty"; } if (model.CargoType == null || model.CargoType == "") { model.CargoType = "empty"; } List <Cargo> cargoes = new List <Cargo>(); cargoes = db.Cargoes.Where(c => (c.Name == model.Name || model.Name == "empty") && (model.PricePerKilometer == 0 || c.PricePerKilometer == model.PricePerKilometer) && (model.Weight == 0 || c.Weight == model.Weight) && (fromLocation.LocationId == 0 || c.FromLocationId == fromLocation.LocationId) && (forLocation.LocationId == 0 || c.ForLocationId == forLocation.LocationId) && (model.TransportType == "empty" || c.TransportType.ToString() == model.TransportType) && (model.CargoType == "empty" || c.CargoType.ToString() == model.CargoType) && c.CarrierId == null).ToList(); List <Cargo> results = cargoes.Where(c => c.CarrierId == null).ToList(); ModelStaticList.cargoModels = new List <CargoView>(); foreach (var cargo in results) { CargoView cargoView = new CargoView(); cargoView.Id = cargo.CargoId; cargoView.Name = cargo.Name; cargoView.PricePerKilometer = cargo.PricePerKilometer; cargoView.Weight = cargo.Weight; Location fromLoc = db.Locations.Where(l => l.LocationId == cargo.FromLocationId).First(); Location forLoc = db.Locations.Where(l => l.LocationId == cargo.ForLocationId).First(); cargoView.FromLocation = fromLoc.Name; cargoView.ForLocation = forLoc.Name; cargoView.CargoType = cargo.CargoType.ToString(); cargoView.TransportType = cargo.TransportType.ToString(); Forwarder forwarderIn = db.Fowarders.Where(f => f.ForwarderId == cargo.ForwarderId).First(); cargoView.Forwarder = forwarderIn.Name; ModelStaticList.cargoModels.Add(cargoView); } return(RedirectToAction("SearchedCargoes")); }