コード例 #1
0
 public ActionResult Edit(Recipe recipe, HttpPostedFileBase image)
 {
     if (ModelState.IsValid)
     {
         if (image != null)
         {
             recipe.ImageMimeType = image.ContentType;
             recipe.ImageData = new byte[image.ContentLength];
             image.InputStream.Read(recipe.ImageData, 0, image.ContentLength);
         }
         recipe.UpdateDatetime = DateTime.Now;
         unitOfWork.RecipeRepository.Update(recipe);
         unitOfWork.Save();
         return RedirectToAction("Index");
     }
     ViewBag.CategoryId = new SelectList(unitOfWork.CategoryRepository.Get().ToList(), "CategoryId", "CategoryName", recipe.CategoryId);
     ViewBag.RegionId = new SelectList(unitOfWork.RegionRepository.Get().ToList(), "RegionId", "RegionName", recipe.RegionId);
     return View(recipe);
 }
コード例 #2
0
        public async Task<ActionResult> Create(Recipe recipe, HttpPostedFileBase image = null)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    recipe.ImageMimeType = image.ContentType;
                    recipe.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(recipe.ImageData, 0, image.ContentLength);
                }
                recipe.CreateDateTime = DateTime.Now;
                recipe.UpdateDatetime = DateTime.Now;

                var user = await IdentityHelpers.UserManager.FindByNameAsync(User.Identity.Name);
                recipe.UserId = user.Id;

                unitOfWork.RecipeRepository.Insert(recipe);
                unitOfWork.Save();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryId = new SelectList(unitOfWork.CategoryRepository.Get().ToList(), "CategoryId", "CategoryName", recipe.CategoryId);
            ViewBag.RegionId = new SelectList(unitOfWork.RegionRepository.Get().ToList(), "RegionId", "RegionName", recipe.RegionId);
            return View(recipe);
        }