#19621: Implementing a view model to avoid the preliminary update to the part.

Work Item: 19621

--HG--
branch : 1.x
This commit is contained in:
Sipke Schoorstra
2013-04-18 11:23:31 +02:00
parent ca79db0072
commit b4b341d0a9
4 changed files with 26 additions and 6 deletions

View File

@@ -7,6 +7,7 @@ using Orchard.ContentManagement.Handlers;
using Orchard.Localization;
using Orchard.MediaProcessing.Models;
using Orchard.MediaProcessing.Services;
using Orchard.MediaProcessing.ViewModels;
using Orchard.Utility.Extensions;
namespace Orchard.MediaProcessing.Drivers {
@@ -32,21 +33,34 @@ namespace Orchard.MediaProcessing.Drivers {
}
protected override DriverResult Editor(ImageProfilePart part, dynamic shapeHelper) {
var viewModel = new ImageProfileViewModel {
Name = part.Name
};
return ContentShape("Parts_MediaProcessing_ImageProfile_Edit",
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: part, Prefix: Prefix));
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: viewModel, Prefix: Prefix));
}
protected override DriverResult Editor(ImageProfilePart part, IUpdateModel updater, dynamic shapeHelper) {
var currentName = part.Name;
updater.TryUpdateModel(part, Prefix, null, null);
if (String.IsNullOrWhiteSpace(part.Name)) {
var viewModel = new ImageProfileViewModel();
// It would be nice if IUpdateModel provided access to the IsValid property of the Controller, instead of having to track a local flag.
var isValid = updater.TryUpdateModel(viewModel, Prefix, null, null);
if (String.IsNullOrWhiteSpace(viewModel.Name)) {
updater.AddModelError("Name", T("The Name can't be empty."));
isValid = false;
}
if (currentName != part.Name && _imageProfileService.GetImageProfileByName(part.Name) != null) {
if (currentName != viewModel.Name && _imageProfileService.GetImageProfileByName(viewModel.Name) != null) {
updater.AddModelError("Name", T("A profile with the same Name already exists."));
isValid = false;
}
if (part.Name != part.Name.ToSafeName()) {
if (viewModel.Name != viewModel.Name.ToSafeName()) {
updater.AddModelError("Name", T("The Name can only contain letters and numbers without spaces"));
isValid = false;
}
if (isValid) {
part.Name = viewModel.Name;
}
return Editor(part, shapeHelper);

View File

@@ -131,6 +131,7 @@
<Compile Include="ViewModels\AdminIndexViewModel.cs" />
<Compile Include="ViewModels\FilterAddViewModel.cs" />
<Compile Include="ViewModels\FilterEditViewModel.cs" />
<Compile Include="ViewModels\ImageProfileViewModel.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Admin\Edit.cshtml" />

View File

@@ -0,0 +1,5 @@
namespace Orchard.MediaProcessing.ViewModels {
public class ImageProfileViewModel {
public string Name { get; set; }
}
}

View File

@@ -1,4 +1,4 @@
@model Orchard.MediaProcessing.Models.ImageProfilePart
@model Orchard.MediaProcessing.ViewModels.ImageProfileViewModel
<fieldset>
@Html.LabelFor(m => m.Name, T("Name"))
@Html.TextBoxFor(m => m.Name, new {@class = "text"})