public IActionResult Create(int measurementId)
        {
            var model = new CalculatorEditViewModel();

            model.MeasurementId = measurementId;
            model.Calculators   = GetCalculatorsDropDown();

            return(View("Edit", model));
        }
예제 #2
0
        public async Task <IActionResult> Edit(CalculatorEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Calculator calculator;
                    if (model.Id == 0)
                    {
                        calculator             = new Calculator();
                        calculator.Measurement = _context.Measurements.FirstOrDefault(m => m.Id == model.MeasurementId);

                        _pageContext.Title = "Create calculator";
                    }
                    else
                    {
                        calculator = _context.Calculators
                                     .Include(m => m.Measurement)
                                     .FirstOrDefault(s => s.Id == model.Id);

                        _pageContext.Title = "Edit calculator " + calculator.Name;
                    }

                    Mapper.Map(model, calculator);

                    if (model.Id == 0)
                    {
                        _context.Calculators.Add(calculator);
                    }

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (model.Id > 0 && !CalculatorExists(model.Id))
                    {
                        _pageContext.Title = "Calculator not found";
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction("Edit", "Measurements", new { id = model.MeasurementId }));
            }

            model.Calculators = GetCalculatorsDropDown(model.Id.ToString());

            return(View(model));
        }
 public async Task <IActionResult> Create(CalculatorEditViewModel calculator)
 {
     return(await Edit(calculator));
 }