示例#1
0
        public async Task <ActionResult> New(SurveyModel contentModel)
        {
            var temporarySurveyModel = GetTemporarySurveyModel();

            if (temporarySurveyModel == null)
            {
                return(this.RedirectToAction("New"));
            }

            if (temporarySurveyModel.Questions == null || temporarySurveyModel.Questions.Count <= 0)
            {
                this.ModelState.AddModelError("ContentModel.Questions", string.Format(CultureInfo.InvariantCulture, "Please add at least one question to the survey."));
            }

            contentModel.Questions = temporarySurveyModel.Questions;
            if (!this.ModelState.IsValid)
            {
                var model = await this.CreateTenantPageViewDataAsync(contentModel);

                model.Title = "New Survey";
                SaveTemporarySurveyModel(temporarySurveyModel);
                return(this.View(model));
            }

            contentModel.TenantId = this.TenantId;
            try
            {
                await this.surveyStore.SaveSurveyAsync(contentModel.ToSurvey());
            }
            catch (StorageException ex)
            {
                TraceHelper.TraceError(ex.TraceInformation());
                throw;
            }

            ClearTemporarySurvey();
            return(this.RedirectToAction("Index"));
        }
        public ActionResult New(SurveyModel contentModel)
        {
            var cachedSurvey = (SurveyModel)this.TempData[CachedSurvey];

            if (cachedSurvey == null)
            {
                return(this.RedirectToAction("New"));
            }

            if (cachedSurvey.Questions == null || cachedSurvey.Questions.Count <= 0)
            {
                this.ModelState.AddModelError("ContentModel.Questions", string.Format(CultureInfo.InvariantCulture, "Please add at least one question to the survey."));
            }

            if ((cachedSurvey.Extensions != null) &&
                (cachedSurvey.Extensions.Count != contentModel.Extensions.Count))
            {
                this.ModelState.AddModelError("ContentModel.Extensions", string.Format(CultureInfo.InvariantCulture, "The number of custom properties don't match with the custom model."));
            }

            if (cachedSurvey.Extensions != null)
            {
                for (int i = 0; i < cachedSurvey.Extensions.Count; i++)
                {
                    contentModel.Extensions[i].PropertyName = cachedSurvey.Extensions[i].PropertyName;
                    if (string.IsNullOrWhiteSpace(contentModel.Extensions[i].PropertyValue))
                    {
                        this.ModelState.AddModelError("ContentModel.Extensions", string.Format(CultureInfo.InvariantCulture, "You need to provide a value for extended property '{0}'.", contentModel.Extensions[i].PropertyName.SplitWords()));
                    }
                }
            }

            contentModel.Questions = cachedSurvey.Questions;
            if (!this.ModelState.IsValid)
            {
                var model = this.CreateTenantPageViewData(contentModel);
                model.Title = "New Survey";
                this.TempData[CachedSurvey] = cachedSurvey;
                return(this.View(model));
            }

            contentModel.Tenant = this.TenantName;
            try
            {
                string assemblyFile, typeNamespace;
                if (this.HasModelExtensionAssembly(this.TenantName, out assemblyFile, out typeNamespace))
                {
                    var modelExtension = ExtensibilityTypeResolver.GetInstanceFrom(assemblyFile, typeNamespace, typeof(Survey));
                    ExtensibilityTypeMapper.SetModelExtensionProperties(modelExtension, contentModel.Extensions);
                    this.surveyStore.SaveSurveyExtension(contentModel, modelExtension);
                }

                this.surveyStore.SaveSurvey(contentModel.ToSurvey());
            }
            catch (DataServiceRequestException ex)
            {
                var dataServiceClientException = ex.InnerException as DataServiceClientException;
                if (dataServiceClientException != null)
                {
                    if (dataServiceClientException.StatusCode == 409)
                    {
                        TraceHelper.TraceWarning(ex.TraceInformation());

                        var model = this.CreateTenantPageViewData(contentModel);
                        model.Title = "New Survey";
                        this.ModelState.AddModelError("ContentModel.Title", string.Format(CultureInfo.InvariantCulture, "The name '{0}' is already in use. Please choose another name.", model.ContentModel.Title));
                        return(this.View(model));
                    }
                }

                TraceHelper.TraceError(ex.TraceInformation());

                throw;
            }

            this.TempData.Remove(CachedSurvey);
            return(this.RedirectToAction("Index"));
        }