예제 #1
0
        public virtual async Task <ActionResult> Create(ParticipantModel viewModel, HttpPostedFileBase upload)
        {
            ViewBag.CountryID       = COList();
            ViewBag.ParticipantType = PTList();
            ViewBag.SportType       = SPList();
            if (ModelState.IsValid)
            {
                if (!_participant.IsExist(viewModel.Name, viewModel.SportTypeID, viewModel.CountryID))
                {
                    var validImageTypes = new string[]
                    {
                        "image/gif",
                        "image/jpeg",
                        "image/jpg",
                        "image/png"
                    };
                    var httpPostedFileBases = upload as HttpPostedFileBase;
                    if (upload != null && !validImageTypes.Contains(upload.ContentType))
                    {
                        ViewBag.Message = " پسوند تصویر انتخاب شده غیر مجاز است";
                        return(View());
                    }
                    const string uploadDir = "~/Uploads/Participant";
                    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 item = new ParticipantModel
                    {
                        Name              = viewModel.Name,
                        SportType         = viewModel.SportType,
                        SportTypeID       = viewModel.SportTypeID,
                        Logo              = fileName,
                        CountryID         = viewModel.CountryID,
                        ParticipantTypeID = viewModel.ParticipantTypeID
                    };
                    _participant.Add(item);
                    await _uow.SaveChangesAsync();

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