Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("BikeId,BikeName,BikeNo,ImageName")] Bike bike)
        {
            if (ModelState.IsValid)
            {
                _context.Add(bike);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(bike));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(Bike bike)
        {
            if (ModelState.IsValid)   // it will check whether our form is valid or not
            {
                if (bike.BikeImage != null)
                {
                    string rootPath   = env.WebRootPath;                  // get the root directory i.e. /wwwroot/
                    string uniqueName = Guid.NewGuid().ToString();

                    string fileName   = uniqueName + bike.BikeImage.FileName;  // file uploaded name
                    string uploadPath = rootPath + "/Images/" + fileName;      // creating upload path
                    bike.ImageName = fileName;                                 // assing file name to bike>imagename
                    using (var filestream = new FileStream(uploadPath, FileMode.Create))
                    {
                        await bike.BikeImage.CopyToAsync(filestream);
                    }
                }
                db.Add(bike);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(bike));
        }