예제 #1
0
        public virtual async Task <ActionResult> Create(TournamentModel viewModel, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                if (!_tournament.IsExist(viewModel.Title))
                {
                    var validImageTypes = new string[]
                    {
                        "image/gif",
                        "image/jpeg",
                        "image/jpg",
                        "image/png"
                    };
                    var httpPostedFileBases = upload as HttpPostedFileBase;
                    if (upload != null && !validImageTypes.Contains(upload.ContentType))
                    {
                        ModelState.AddModelError("", " پسوند تصویر انتخاب شده غیر مجاز است");
                        return(View());
                    }

                    if (upload == null)
                    {
                        ModelState.AddModelError("", " بارگزاری تصویر اجباری است");
                        return(View());
                    }

                    const string uploadDir = "~/Uploads/Tournament";
                    string       fileName  = DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second + "" + Path.GetExtension(upload.FileName);
                    var          imagePath = Path.Combine(Server.MapPath(uploadDir), fileName);
                    upload.SaveAs(imagePath);

                    var tournam = new TournamentModel
                    {
                        Title = viewModel.Title,
                        Logo  = fileName,
                    };
                    _tournament.Add(tournam);
                    await _uow.SaveChangesAsync();

                    ViewBag.Status = true;
                    return(View());
                }
                else
                {
                    ViewBag.Message = "عنوان وارد شده تکراری است.";
                    return(View());
                }
            }
            else
            {
                return(View());
            }
        }