示例#1
0
        public async Task <IActionResult> Create([Bind("CategoryId,CategoryName,Description")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
        public async Task <IActionResult> Create([Bind("OrderId,FirstName,LastName,AddressLine1,AddressLine2,ZipCode,State,Country,PhoneNumber,Email,OrderTotal,OrderPlaced")] Order order)
        {
            if (ModelState.IsValid)
            {
                _context.Add(order);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(order));
        }
示例#3
0
        public async Task <IActionResult> Create([Bind("PhoneId,Name,ShortDescription,Price,ImageUrl,ImageThumbnailUrl,IsPreferredPhone,CategoryId")] Phone phone, IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return(Content("file not selected"));
            }
            if (ModelState.IsValid)
            {
                _context.Add(phone);

                //< get Path >

                string pathRoot = __appEnvironment.WebRootPath;

                string pathToImages = pathRoot + "\\images\\" + file.FileName;
                string imageUrl     = "\\images\\" + file.FileName;
                //</ get Path >

                //< Copy File to Target >


                using (var stream = new FileStream(pathToImages, FileMode.Create))

                {
                    await file.CopyToAsync(stream);
                }
                //</ Copy File to Target >
                if (pathToImages != null)
                {
                    phone.ImageUrl = imageUrl;
                }
                //< output >

                ViewData["FilePath"] = imageUrl;
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryId", phone.CategoryId);
            return(View(phone));
        }