Esempio n. 1
0
        public ActionResult Edit(PollQuestion model, HttpPostedFileBase questionImage, List <HttpPostedFileBase> answerImages)
        {
            if (questionImage != null && questionImage.ContentLength > 0)
            {
                string fileName = questionImage.FileName;
                using (new Impersonator("uploaduser", "", "Upload@@123"))
                {
                    string currentDomain = System.Configuration.ConfigurationManager.AppSettings["CurrentDomain"];

                    string catFolder = "UploadedImages/ArticleCategory";
                    string filePath  = System.Configuration.ConfigurationManager.AppSettings[currentDomain] + @"\" + catFolder + @"\" + fileName;
                    if (System.IO.File.Exists(filePath))
                    {
                        if (new FileInfo(filePath).Length != questionImage.ContentLength)
                        {
                            fileName = Guid.NewGuid().ToString() + questionImage.FileName;
                            questionImage.SaveAs(filePath);
                        }
                    }
                    else
                    {
                        questionImage.SaveAs(filePath);
                    }
                    model.Image = currentDomain + "/" + catFolder + "/" + fileName;
                }
            }
            var existAnswers = _pollRepository.GetAnswers(model.Id);

            if (model.Answers != null)
            {
                //remove answer which not containt content
                for (int i = model.Answers.Count - 1; i >= 0; i--)
                {
                    var answer = model.Answers.ElementAt(i);

                    if (string.IsNullOrEmpty(answer.Answer))
                    {
                        model.Answers.Remove(answer);
                        answerImages.RemoveAt(i);
                        if (answer.AnswerId != 0) //exist answer
                        {
                            //remove user answers
                            _pollRepository.DeleleAnswer(answer.QuestionId, answer.AnswerId);
                        }
                    }
                }

                //set answer id for new answer
                for (int i = 0; i < model.Answers.Count; i++)
                {
                    if (model.Answers.ElementAt(i).AnswerId == 0)
                    {
                        model.Answers.ElementAt(i).AnswerId = model.Answers.Max(a => a.AnswerId) + 1;
                    }
                }
            }
            if (answerImages != null)
            {
                for (int i = 0; i < model.Answers.Count; i++)
                {
                    if (answerImages.ElementAt(i) != null)
                    {
                        var    answerImage = answerImages.ElementAt(i);
                        string fileName    = answerImage.FileName;
                        //string fileName = Convert.ToInt32((DateTime.Now - new DateTime(2010, 01, 01)).TotalSeconds).ToString() + "_" + profileFile.FileName;
                        using (new Impersonator("uploaduser", "", "Upload@@123"))
                        {
                            string currentDomain = System.Configuration.ConfigurationManager.AppSettings["CurrentDomain"];

                            string catFolder = "UploadedImages/ArticleCategory";
                            string filePath  = System.Configuration.ConfigurationManager.AppSettings[currentDomain] + @"\" + catFolder + @"\" + fileName;
                            if (System.IO.File.Exists(filePath))
                            {
                                if (new FileInfo(filePath).Length != answerImage.ContentLength)
                                {
                                    fileName = Guid.NewGuid().ToString() + answerImage.FileName;
                                    answerImage.SaveAs(filePath);
                                }
                            }
                            else
                            {
                                answerImage.SaveAs(filePath);
                            }
                            model.Answers.ElementAt(i).Image = currentDomain + "/" + catFolder + "/" + fileName;
                        }
                    }
                }
            }

            _pollRepository.InsertOrUpdate(model);
            _pollRepository.Save();
            var    question      = _pollRepository.QuestionAnswers(model.Id);
            string adminquestion = Utilities.RenderRazorViewToString(this, "~/Views/Poll/_AdminQuestion.cshtml", question);

            return(Json(new { success = true, data = adminquestion, questionid = model.Id }));
        }