Пример #1
0
 public ActionResult AddHair()
 {
     using (AppContext context = AppContext.Create())
     {
         var           colors  = context.HairColors.AsNoTracking().ToList();
         var           lengths = context.HairLengths.AsNoTracking().ToList();
         HairViewModel model   = new HairViewModel()
         {
             HairColors = colors, HairLengths = lengths
         };
         return(PartialView(model));
     }
 }
Пример #2
0
        public JsonResult AddHair([System.Web.Http.FromBody] HairViewModel model)
        {
            using (AppContext context = AppContext.Create())
            {
                if (!ModelState.IsValid)
                {
                    return(Json(false));
                }
                //byte[] bytes;

                //using (BinaryReader br = new BinaryReader(model.PostedImage.InputStream))
                //{
                //    bytes = br.ReadBytes(model.PostedImage.ContentLength);
                //}

                //ImageEntity image = new ImageEntity() { Name = model.PostedImage.FileName, ContentType = model.PostedImage.ContentType, Data = bytes };
                HairItem hairItem = new HairItem()
                {
                    Name = model.Name, Price = model.Price, HairImage = model.PostedImageData, HairType = model.HairType
                };
                foreach (HairLength length in model.HairLengths)
                {
                    hairItem.HairLengths.Add(length);
                }

                foreach (HairColor color in model.HairColors)
                {
                    hairItem.HairColors.Add(color);
                }

                //context.Images.Add(image);
                context.HairItems.Add(hairItem);
                context.SaveChanges();
                return(Json(true));
            }
        }