コード例 #1
0
        public ActionResult AddItem(ItemCreateVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var binaryReader = new BinaryReader(model.ImageUpload.InputStream))
                    {
                        model.Image = binaryReader.ReadBytes(model.ImageUpload.ContentLength);
                    }

                    var item = Mapper.Map <Item>(model);

                    bool isSaved = _itemManager.Save(item);
                    if (isSaved)
                    {
                        return(RedirectToAction("AddItem"));
                    }
                }
            }
            catch (Exception exception)
            {
                ModelState.AddModelError("", exception.Message);
                model.ItemCategories = _ItemCategoryManager.GetAll();
                return(View(model));
            }
            model.ItemCategories = _ItemCategoryManager.GetAll();
            return(View(model));
        }
コード例 #2
0
        public ActionResult AddItem()
        {
            var model = new ItemCreateVM();

            model.ItemCategories = _ItemCategoryManager.GetAll();
            return(View(model));
        }
コード例 #3
0
        // GET: Item/Create
        public ActionResult Create()
        {
            var model = new ItemCreateVM();

            model.ItemCategories = _itemCategoryManager.GetAll();
            model.ChildItems     = _itemManager.GetAll();
            return(View(model));
        }
コード例 #4
0
ファイル: ItemsController.cs プロジェクト: ELI7VH/nude-mvc
        public async Task <ActionResult> Post(ItemCreateVM item)
        {
            // parse body
            await db.Item.AddAsync(new Item {
                Name     = item.Name,
                Value    = item.Value,
                Category = item.Category
            });

            await db.SaveChangesAsync();

            // TODO: socket
            return(Ok());
        }
コード例 #5
0
        public ActionResult Create(ItemCreateVM model)
        {
            try
            {
                var  item    = Mapper.Map <Item>(model);
                bool isSaved = _itemManager.Save(item);
                if (isSaved)
                {
                    return(RedirectToAction("Create"));
                }
            }
            catch (Exception exception)
            {
                ModelState.AddModelError("", exception.Message);

                model.ItemCategories = _itemCategoryManager.GetAll();
                model.ChildItems     = _itemManager.GetAll();
                return(View(model));
            }
            model.ItemCategories = _itemCategoryManager.GetAll();
            model.ChildItems     = _itemManager.GetAll();
            return(View(model));
        }
コード例 #6
0
        public ActionResult Create([Bind(Include = "Name,Blurb,PictureFileName,DescriptionText,PropertyText,HistoryText, Artist, IsSecret")] ItemCreateVM item,
                                   HttpPostedFileBase picture,
                                   List <short> tags,
                                   string submit)
        {
            //so it seems that - at least for none complicated associations like tags - I am actually able to just use the base submit

            //extra logic here for when I allow the empty placeholders during save actions. Check if the action is save and then stop her if it isn't
            #region Save or Publish?
            switch (submit)
            {
            case "Save Progress":
            case "Un-Publish":
                item.IsPublished = false;
                break;

            case "Publish":
            case "Save":
                item.IsPublished = true;
                break;
            }
            #endregion

            #region Pre-model picture check
            if (picture != null)
            {
                string[] goodExts = { ".jpg", ".jpeg", ".gif", ".png" };
                string   imgName  = picture.FileName;

                var    length = picture.ContentLength;
                string ext    = imgName.Substring(imgName.LastIndexOf('.'));

                if (!goodExts.Contains(ext.ToLower()))
                {
                    ModelState.AddModelError("PictureFileName", "You have submitted a incorrect file type for your portrait. Please use either: .jpg, .jpeg, .gif, or .png");
                }

                if (item.Artist == null)
                {
                    ModelState.AddModelError("Artist", "Katherine, you're trying to submit something with a picture without an artist. That's a no-no! But seriously, if something came up that means you need to change this rule, you know who to call.");
                }
            }
            #endregion

            if (ModelState.IsValid)
            {
                #region Info

                Info info = new Info
                {
                    InfoTypeId   = 7, //<-------------------Info Type ID. CHANGE UPON COPY
                    IdWithinType = null,
                    Blurb        = item.Blurb,
                    Name         = item.Name,
                    IsPublished  = item.IsPublished,
                    IsSecret     = item.IsSecret
                };
                db.Infos.Add(info);
                db.SaveChanges(); //this has to go here in order to ensure that the infoId short below is accurate. Also at this point I am doing no further gets on validity so there is no point to not saving
                #endregion

                short infoId = db.Infos.Max(i => i.InfoId);

                #region Adding Tags
                if (tags != null)
                {
                    foreach (short t in tags)
                    {
                        InfoTag infoTag = new InfoTag {
                            InfoId = infoId, TagId = t
                        };
                        db.InfoTags.Add(infoTag);
                    }
                }
                #endregion

                #region Image uploads
                string[] goodExts = { ".jpg", ".jpeg", ".gif", ".png" };
                item.PictureFileName = "default.jpg";
                if (picture != null)
                {
                    string imgName = picture.FileName;

                    string ext = imgName.Substring(imgName.LastIndexOf('.'));

                    if (goodExts.Contains(ext.ToLower()))
                    {
                        imgName = "item-" + infoId.ToString() + ext;

                        picture.SaveAs(Server.MapPath("~/Content/img/item/" + imgName));
                    }
                    item.PictureFileName = imgName;
                }
                #endregion

                #region Item
                Item daItem = new Item
                {
                    InfoId          = infoId,
                    Name            = item.Name,
                    PictureFileName = item.PictureFileName,
                    DescriptionText = item.DescriptionText,
                    PropertyText    = item.PropertyText,
                    HistoryText     = item.HistoryText,
                    IsPublished     = item.IsPublished,
                    Artist          = item.Artist
                };
                db.Items.Add(daItem);
                db.SaveChanges();
                #endregion

                //now give the info the idWithinType
                #region give info the IdWithinType
                short maxi = db.Items.Max(i => i.ItemId);
                info.IdWithinType    = maxi;
                db.Entry(info).State = EntityState.Modified;
                db.SaveChanges();
                #endregion

                return(RedirectToAction("Details", new { id = maxi }));
            }

            //if model is not valid
            ViewBag.Tags = new MultiSelectList(db.Tags, "TagId", "TagName", tags);

            if (picture != null)
            {
                ModelState.AddModelError("PictureFileName", "Hey, there was some error, so you have to re-upload the picture");
            }
            ModelState.AddModelError("", "Something has gone wrong. Look for red text to see where is went wrong");
            return(View(item));
        }