Esempio n. 1
0
        //
        //  GET: /Manage/Create new plan or edit plan
        public ActionResult CreateNewPlan(int id = 0)
        {
            if (id != 0)
            {
                var context = HttpContext.GetOwinContext().Get <ApplicationDbContext>();

                var planRecord = (from c in context.PlanRecords
                                  .Include("From")
                                  .Include("To")
                                  where c.Id == id
                                  select c).FirstOrDefault();
                CreateNewPlanViewModel model = new CreateNewPlanViewModel()
                {
                    Id     = planRecord.Id,
                    From   = planRecord.From.Name,
                    To     = planRecord.To.Name,
                    Period = planRecord.Month,
                    Plan   = planRecord.Number
                };
                ViewBag.Title = "Edit plan";
                return(View("CreateNewPlan", model));
            }
            ViewBag.Title = "Create new plan for delivery";
            return(View());
        }
Esempio n. 2
0
        public async Task <ActionResult> CreateNewPlan(CreateNewPlanViewModel model)
        {
            var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

            var context = HttpContext.GetOwinContext().Get <ApplicationDbContext>();

            if (ModelState.IsValid)
            {
                if (model.Id == 0)
                {
                    PlanRecord planRecord = new PlanRecord()
                    {
                        From   = GetCity(model.From),
                        To     = GetCity(model.To),
                        Month  = model.Period,
                        Number = model.Plan
                    };
                    context.PlanRecords.Add(planRecord);
                    context.SaveChanges();
                    return(RedirectToAction("Index", new { Message = ManageMessageId.AddNewPeriod }));
                }
                var planRecordForUpdate = (from c in context.PlanRecords
                                           .Include("From")
                                           .Include("To")
                                           where c.Id == model.Id
                                           select c).FirstOrDefault();
                planRecordForUpdate.From   = GetCity(model.From);
                planRecordForUpdate.To     = GetCity(model.To);
                planRecordForUpdate.Month  = model.Period;
                planRecordForUpdate.Number = model.Plan;
                context.SaveChanges();
                return(RedirectToAction("Index", new { Message = ManageMessageId.UpdatePlanRecord }));
            }

            // Это сообщение означает наличие ошибки; повторное отображение формы
            return(View(model));
        }