Beispiel #1
0
        public ActionResult Add()
        {
            var model = new CardInfoAddViewModel();

            var setsRepo    = SetsRepositoryFactory.GetRepository();
            var artistsRepo = ArtistRepositoryFactory.GetRepository();

            model.Sets     = new SelectList(setsRepo.GetAll(), "SetId", "SetName");
            model.Artists  = new SelectList(artistsRepo.GetAll(), "ArtistId", "ArtistName");
            model.CardInfo = new CardInfo();
            return(View(model));
        }
Beispiel #2
0
        public ActionResult Add(CardInfoAddViewModel model)
        {
            if (ModelState.IsValid)
            {
                var repo = CardInfoRepositoryFactory.GetRepository();

                try
                {
                    var savepath = Server.MapPath("~/Images");

                    string fileName  = Path.GetFileNameWithoutExtension(model.ImageUpload.FileName);
                    string extension = Path.GetExtension(model.ImageUpload.FileName);

                    var filePath = Path.Combine(savepath, fileName + extension);

                    int counter = 1;
                    while (System.IO.File.Exists(filePath))
                    {
                        filePath = Path.Combine(savepath, fileName + counter.ToString() + extension);
                        counter++;
                    }

                    model.ImageUpload.SaveAs(filePath);

                    model.CardInfo.CardArtURL = Path.GetFileName(filePath);

                    repo.Insert(model.CardInfo);

                    return(RedirectToAction("Edit", new { id = model.CardInfo.CardId }));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                var setsRepo = SetsRepositoryFactory.GetRepository();
                model.Sets = new SelectList(setsRepo.GetAll(), "SetId", "SetName");
                var artistsRepo = ArtistRepositoryFactory.GetRepository();
                model.Artists = new SelectList(artistsRepo.GetAll(), "ArtistId", "ArtistName");
                return(View(model));
            }
        }