public ActionResult Create(int id) { var context = new Context(); var userId = int.Parse(HttpContext.User.Identity.Name); var exist = (from r in context.CarrierRating join c in context.Carriers on r.CarrierId equals c.Id where r.CarrierId == id && r.UserId == userId select r).Any(); if (exist) { return RedirectToAction("Edit", new { id = id }); } var carrier = (from p in context.Carriers where p.Id == id select p).FirstOrDefault(); if (carrier == null) { return RedirectToAction("Index", "Carriers"); } var viewModel = new RatingViewModel { CarrierId = carrier.Id, CarrierName = carrier.Name }; return View(viewModel); }
public ActionResult Create(RatingViewModel viewModel) { if (ModelState.IsValid) { var userId = int.Parse(HttpContext.User.Identity.Name); var rating = new CarrierRating { CarrierId = viewModel.CarrierId, UserId = userId, Rating = viewModel.Rating.Value }; var context = new Context(); rating.Create(); context.CarrierRating.Add(rating); context.SaveChanges(); return RedirectToAction("Index", "Carriers"); } return View(viewModel); }
public ActionResult Edit(RatingViewModel viewModel) { if (ModelState.IsValid) { var userId = int.Parse(HttpContext.User.Identity.Name); var context = new Context(); var rating = context.CarrierRating .FirstOrDefault(x => x.CarrierId == viewModel.CarrierId && x.UserId == userId); if (rating == null) { return RedirectToAction("Index", "Carriers"); } rating.Rating = viewModel.Rating.Value; rating.Update(); context.SaveChanges(); return RedirectToAction("Index", "Carriers"); } return View(viewModel); }