// GET: SnackInfoes public async Task <IActionResult> IndexAsync(int?pageNumber, string searchString) { int total = _context.SnackInfo.Count(); int pagecount = total / 10 + 1; if (pagecount > 10) { pagecount = 10; } PaginatedList <SnackInfo> snackInfos = null; if (searchString != null) { snackInfos = await PaginatedList <SnackInfo> .CreateAsync(_context.SnackInfo.Where(s => s.Name.Contains(searchString) && s.Status != -1).OrderByDescending(s => s.Price), pageNumber ?? 1, 10); } else { snackInfos = await PaginatedList <SnackInfo> .CreateAsync(_context.SnackInfo.Where(s => s.Status != -1).OrderByDescending(s => s.Price), pageNumber ?? 1, 10); } SnackInfoViewModels snackInfoViewModels = new SnackInfoViewModels() { SnackInfos = snackInfos, PageIndex = pageNumber ?? 1, PageTotal = pagecount }; // int pageSize = 3; return(View(snackInfoViewModels)); // return View(snackInfoViewModels); }
public async Task <IActionResult> Create(SnackInfoViewModels snackInfoViewModels) { int num = snackInfoViewModels.SnackInfo.SnackNum; if (ModelState.IsValid) { string file_name = $"{snackInfoViewModels.SnackInfo.SnackNum}_{DateTime.Now.ToString("yyyymmddHHmmss")}.jpg"; using (var fileStream = new FileStream(Path.Combine(_dir, file_name), FileMode.Create, FileAccess.Write)) { snackInfoViewModels.FormFile.CopyTo(fileStream); } snackInfoViewModels.SnackInfo.ImgUrl = relative_path + file_name; long category_id = _context.SnackCategory.FirstOrDefault(c => c.CategoryName == snackInfoViewModels.CategoryName).CategoryNum; snackInfoViewModels.SnackInfo.CategoryId = category_id; snackInfoViewModels.SnackInfo.CreateTime = DateTime.Now; _context.Add(snackInfoViewModels.SnackInfo); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(snackInfoViewModels)); }
// GET: SnackInfoes/Create public IActionResult Create() { // Use LINQ to get list of category. IQueryable <string> categoryQuery = from m in _context.SnackCategory where m.Status == 1 orderby m.CategoryNum select m.CategoryName; int SnackNum = _context.SnackInfo.Max(s => s.SnackNum) + 1; SnackInfoViewModels snackInfoViewModels = new SnackInfoViewModels() { CategoriesName = new SelectList(categoryQuery.ToList()), SnackNum = SnackNum, }; return(View(snackInfoViewModels)); }