From 5aecec312732f328f662076802b8678eeeeef2fc Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Thu, 16 Jul 2015 14:58:57 +0100 Subject: [PATCH] Fixed modelstate persistence on import screen in case of validation errors. --- .../ImportActions/UploadRecipeAction.cs | 26 +++++++++++++------ .../ImportActions/UploadRecipe.cshtml | 22 +++++++--------- .../Providers/Executors/ContentStep.cs | 12 ++++++--- .../Recipes/Services/IRecipeExecutionStep.cs | 3 ++- .../Recipes/Services/RecipeExecutionStep.cs | 5 +++- 5 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Providers/ImportActions/UploadRecipeAction.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/Providers/ImportActions/UploadRecipeAction.cs index edde0d76d..efb892858 100644 --- a/src/Orchard.Web/Modules/Orchard.ImportExport/Providers/ImportActions/UploadRecipeAction.cs +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Providers/ImportActions/UploadRecipeAction.cs @@ -24,11 +24,11 @@ namespace Orchard.ImportExport.Providers.ImportActions { private readonly IEnumerable _recipeExecutionSteps; public UploadRecipeAction( - IOrchardServices orchardServices, - IImportExportService importExportService, - ISetupService setupService, - ShellSettings shellSettings, - IFeatureManager featureManager, + IOrchardServices orchardServices, + IImportExportService importExportService, + ISetupService setupService, + ShellSettings shellSettings, + IFeatureManager featureManager, IEnumerable recipeExecutionSteps) { _orchardServices = orchardServices; @@ -86,6 +86,17 @@ namespace Orchard.ImportExport.Providers.ImportActions { } } + var stepUpdater = new Updater(updater, secondHalf => String.Format("{0}.{1}", Prefix, secondHalf)); + + // Update the view model with non-submitted values. + viewModel.SuperUserName = _orchardServices.WorkContext.CurrentSite.SuperUser; + foreach (var stepViewModel in viewModel.RecipeExecutionSteps) { + var step = _recipeExecutionSteps.Single(x => x.Name == stepViewModel.Name); + stepViewModel.DisplayName = step.DisplayName; + stepViewModel.Description = step.Description; + stepViewModel.Editor = step.UpdateEditor(shapeFactory, stepUpdater); + } + if (!isInValid) { // Read recipe file. RecipeDocument = XDocument.Parse(new StreamReader(file.InputStream).ReadToEnd()); @@ -93,20 +104,19 @@ namespace Orchard.ImportExport.Providers.ImportActions { // Update execution steps. var executionStepNames = viewModel.RecipeExecutionSteps.Where(x => x.IsSelected).Select(x => x.Name); - var executionStepsQuery = + var executionStepsQuery = from name in executionStepNames where orchardElement.Element(name) != null let provider = _recipeExecutionSteps.SingleOrDefault(x => x.Name == name) where provider != null select provider; var executionSteps = executionStepsQuery.ToArray(); - var stepUpdater = new Updater(updater, secondHalf => String.Format("{0}.{1}", Prefix, secondHalf)); foreach (var executionStep in executionSteps) { var context = new UpdateRecipeExecutionStepContext { RecipeDocument = RecipeDocument, Step = orchardElement.Element(executionStep.Name) }; - executionStep.UpdateEditor(shapeFactory, stepUpdater, context); + executionStep.UpdateStep(context); } } } diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Views/EditorTemplates/ImportActions/UploadRecipe.cshtml b/src/Orchard.Web/Modules/Orchard.ImportExport/Views/EditorTemplates/ImportActions/UploadRecipe.cshtml index 44d5543f3..92b230593 100644 --- a/src/Orchard.Web/Modules/Orchard.ImportExport/Views/EditorTemplates/ImportActions/UploadRecipe.cshtml +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Views/EditorTemplates/ImportActions/UploadRecipe.cshtml @@ -11,12 +11,13 @@
-
+ @Html.CheckBoxFor(m => m.ResetSite) @Html.LabelFor(m => m.ResetSite, T("Reset Site").ToString(), new {@class = "forcheckbox"}) - @Html.Hint(T("Check this option to reset your site before executing the uploaded recipe.")) -
+ + @Html.Hint(T("Check this option to reset your site before executing the uploaded recipe."))
+
@T("This will delete your database tables. Please consider creating a backup first.")
@Html.LabelFor(m => m.SuperUserPassword, T("Super User Password")) @Html.PasswordFor(m => m.SuperUserPassword, new {@class = "text medium"}) @@ -27,19 +28,19 @@ @Html.PasswordFor(m => m.SuperUserPasswordConfirmation, new {@class = "text medium"}) @Html.Hint(T("Repeat the password to make sure you didn't mistype anything."))
-
@T("This will delete your database tables. Please consider creating a backup first.")
@{ var stepIndex = 0; foreach (var step in Model.RecipeExecutionSteps) { - var stepName = Html.NameFor(m => m.RecipeExecutionSteps[stepIndex].IsSelected).ToString(); - var stepId = stepName.HtmlClassify(); - + var stepId = Html.FieldIdFor(m => m.RecipeExecutionSteps[stepIndex].IsSelected); + if (stepIndex <= Model.RecipeExecutionSteps.Count) { +
+ }
- - + + @Html.CheckBoxFor(m => m.RecipeExecutionSteps[stepIndex].IsSelected) @Html.Hint(step.Description) @@ -48,8 +49,5 @@
stepIndex++; - if (stepIndex < Model.RecipeExecutionSteps.Count) { -
- } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Recipes/Providers/Executors/ContentStep.cs b/src/Orchard.Web/Modules/Orchard.Recipes/Providers/Executors/ContentStep.cs index 36acdc694..177529b6b 100644 --- a/src/Orchard.Web/Modules/Orchard.Recipes/Providers/Executors/ContentStep.cs +++ b/src/Orchard.Web/Modules/Orchard.Recipes/Providers/Executors/ContentStep.cs @@ -33,22 +33,28 @@ namespace Orchard.Recipes.Providers.Executors { get { return T("Provides additional coniguration for the Content recipe step."); } } + public int? BatchSize { get; set; } + public override dynamic BuildEditor(dynamic shapeFactory) { - return UpdateEditor(shapeFactory, null, null); + return UpdateEditor(shapeFactory, null); } - public override dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater, UpdateRecipeExecutionStepContext context) { + public override dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater) { var viewModel = new ContentExecutionStepViewModel(); if (updater != null) { if (updater.TryUpdateModel(viewModel, Prefix, null, null)) { - SetBatchSizeForDataStep(context.Step, viewModel.BatchSize); + BatchSize = viewModel.BatchSize; } } return shapeFactory.EditorTemplate(TemplateName: "ExecutionSteps/Content", Model: viewModel, Prefix: Prefix); } + public override void UpdateStep(UpdateRecipeExecutionStepContext context) { + SetBatchSizeForDataStep(context.Step, BatchSize); + } + // // Import Data. public override void Execute(RecipeExecutionContext context) { diff --git a/src/Orchard/Recipes/Services/IRecipeExecutionStep.cs b/src/Orchard/Recipes/Services/IRecipeExecutionStep.cs index 53fbc29ab..2530190a4 100644 --- a/src/Orchard/Recipes/Services/IRecipeExecutionStep.cs +++ b/src/Orchard/Recipes/Services/IRecipeExecutionStep.cs @@ -8,7 +8,8 @@ namespace Orchard.Recipes.Services { LocalizedString DisplayName { get; } LocalizedString Description { get; } dynamic BuildEditor(dynamic shapeFactory); - dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater, UpdateRecipeExecutionStepContext context); + dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater); + void UpdateStep(UpdateRecipeExecutionStepContext context); void Execute(RecipeExecutionContext context); } diff --git a/src/Orchard/Recipes/Services/RecipeExecutionStep.cs b/src/Orchard/Recipes/Services/RecipeExecutionStep.cs index 6f2bc9dfb..d35d145fa 100644 --- a/src/Orchard/Recipes/Services/RecipeExecutionStep.cs +++ b/src/Orchard/Recipes/Services/RecipeExecutionStep.cs @@ -22,10 +22,13 @@ namespace Orchard.Recipes.Services { return null; } - public virtual dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater, UpdateRecipeExecutionStepContext context) { + public virtual dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater) { return null; } + public virtual void UpdateStep(UpdateRecipeExecutionStepContext context) { + } + public abstract void Execute(RecipeExecutionContext context); } } \ No newline at end of file