Esempio n. 1
0
        public async Task <IActionResult> EditType(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var typeMaker = await _context.TypeMakers
                            .Include(tm => tm.BikeType)
                            .Include(tm => tm.BikeMaker)
                            .FirstAsync(tm => tm.Id == id);

            if (typeMaker == null)
            {
                return(NotFound());
            }

            var typeMakermodel = new TypeMakerViewModel
            {
                Id        = typeMaker.Id,
                ImageUrl  = typeMaker.ImageUrl,
                Types     = _combosHelper.GetComboTypes(),
                BikeType  = typeMaker.BikeType,
                MakerId   = typeMaker.BikeMaker.Id,
                BikeMaker = typeMaker.BikeMaker,
                TypeId    = typeMaker.BikeType.Id
            };

            return(View(typeMakermodel));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddType(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var maker = await _context.BikeMakers.FindAsync(id.Value);

            //.Include(bm => bm.Motorbikes)
            //.Include(bm => bm.TypeMaker)
            //.FirstOrDefaultAsync(bm => bm.Id == id.Value);

            if (maker == null)
            {
                return(NotFound());
            }

            var typeMakermodel = new TypeMakerViewModel
            {
                Types     = _combosHelper.GetComboTypes(),
                MakerId   = maker.Id,
                BikeMaker = maker
            };

            return(View(typeMakermodel));
        }
Esempio n. 3
0
        public async Task <IActionResult> EditType(TypeMakerViewModel model)
        {
            model.BikeMaker = await _context.BikeMakers.FindAsync(model.MakerId);

            model.BikeType = await _context.BikeTypes.FindAsync(model.TypeId);

            if (model.BikeMaker == null)
            {
                model.Types = _combosHelper.GetComboTypes();
                return(View(model));
            }

            if (model.BikeType == null)
            {
                model.Types = _combosHelper.GetComboTypes();
                return(View(model));
            }


            var path = model.ImageUrl;

            if (model.ImageFile != null)
            {
                path = await _imageHelper.UploadImageAsync(model.ImageFile, "BikeTypes");
            }

            var typeMakerEntity = new TypeMakerEntity
            {
                Id        = model.Id,
                ImageUrl  = path,
                BikeMaker = await _context.BikeMakers.FindAsync(model.MakerId),
                BikeType  = await _context.BikeTypes.FindAsync(model.TypeId)
            };

            try
            {
                _context.TypeMakers.Update(typeMakerEntity);
                await _context.SaveChangesAsync();
            }
            catch (Exception err)
            {
                return(RedirectToAction($"Details/{model.MakerId}", new RouteValueDictionary(new { Controller = "BikeMakers", err.Message })));
            }

            return(RedirectToAction($"Details/{model.MakerId}"));
        }