示例#1
0
        public ActionResult Create(SetViewModel set, HttpPostedFileBase[] files)
        {
            //Find logged in user
            User user   = db.Users.Single(s => s.email.Trim().ToLower() == System.Web.HttpContext.Current.User.Identity.Name.Trim().ToLower());
            int  userID = user.id;

            if (ModelState.IsValid)
            {
                SquaresEntities db     = new SquaresEntities();
                List <string>   pieces = new List <string>();

                var squareSet = new Set
                {
                    name         = set.name,
                    userId       = userID,
                    description  = set.description,
                    creationDate = DateTime.Now,
                    //likes = ,
                    //sizeData = ,
                    combinationData = "666",
                };

                //Create folders for set
                string relativePath = "/Images/" + userID + "/" + set.name;
                string path         = Server.MapPath(relativePath);
                Directory.CreateDirectory(path);

                //Lopp through pieces, save, and save path
                foreach (HttpPostedFileBase file in files)
                {
                    if (file != null)
                    {
                        string p = string.Empty;
                        p = Server.MapPath(relativePath + "/");         // make a new path to save images
                        pieces.Add(relativePath + "/" + file.FileName); // the path in database
                        file.SaveAs(p + file.FileName);
                    }
                }

                db.Sets.Add(squareSet);
                db.SaveChanges();
                int setID = squareSet.id;

                CreateSet(setID, pieces);

                return(RedirectToAction("Details", new { id = squareSet.id }));
            }
            //redirect
            return(View(set));
        }