public ActionResult Index()
        {
            this.TempData[CachedSurvey] = null;

            IEnumerable <SurveyModel> surveysForTenant = this.surveyStore
                                                         .GetSurveysByTenant(this.TenantName)
                                                         .Select(s => new SurveyModel(s))
                                                         .ToList();

            if (surveysForTenant.Count() > 0)
            {
                string assemblyFile, typeNamespace;
                if (this.HasModelExtensionAssembly(this.TenantName, out assemblyFile, out typeNamespace))
                {
                    var extensionType   = ExtensibilityTypeResolver.GetTypeFrom(assemblyFile, typeNamespace, typeof(Survey));
                    var modelExtensions = this.surveyStore.GetSurveyExtensionsByTenant(this.TenantName, extensionType);
                    foreach (var modelExtension in modelExtensions)
                    {
                        var survey = surveysForTenant.FirstOrDefault(s => modelExtension.IsChildOf(this.surveyStore.GetStorageKeyFor(s)));
                        if (survey != null)
                        {
                            survey.Extensions = ExtensibilityTypeMapper.GetModelExtensionProperties(modelExtension).ToList();
                        }
                    }
                }
            }

            var model = this.CreateTenantPageViewData(surveysForTenant);

            model.Title = "My Surveys";

            return(this.View(model));
        }
        public ActionResult New()
        {
            var cachedSurvey = (SurveyModel)this.TempData[CachedSurvey];

            if (cachedSurvey == null)
            {
                cachedSurvey = new SurveyModel();  // First time to the page
                string assemblyFile, typeNamespace;
                if (this.HasModelExtensionAssembly(this.TenantName, out assemblyFile, out typeNamespace))
                {
                    var modelExtension = ExtensibilityTypeResolver.GetInstanceFrom(assemblyFile, typeNamespace, typeof(Survey));
                    cachedSurvey.Extensions = ExtensibilityTypeMapper.GetModelExtensionProperties(modelExtension).ToList();
                }
            }

            var model = this.CreateTenantPageViewData(cachedSurvey);

            model.Title = "New Survey";

            this.TempData[CachedSurvey] = cachedSurvey;

            return(this.View(model));
        }
        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"));
        }