From eb9ee643a1c2ca8877a747b3c648b4c3e34b773b Mon Sep 17 00:00:00 2001 From: DamianEdwards Date: Tue, 27 May 2014 16:53:49 -0700 Subject: [PATCH] Fix Spa after breaking change in MVC - Reordered TypeScript targets to before AspNet targets to fix build in VS issue - Refactored the TryUpdateModel workaround in BaseController --- .../Infrastructure/BaseController.cs | 21 +- .../Infrastructure/ForcedModelError.cs | 4 +- src/MusicStore.Spa/MusicStore.Spa.kproj | 702 +++++++++++++++++- 3 files changed, 688 insertions(+), 39 deletions(-) diff --git a/src/MusicStore.Spa/Infrastructure/BaseController.cs b/src/MusicStore.Spa/Infrastructure/BaseController.cs index 0b13f3c738..47247d03a6 100644 --- a/src/MusicStore.Spa/Infrastructure/BaseController.cs +++ b/src/MusicStore.Spa/Infrastructure/BaseController.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.ModelBinding; @@ -12,6 +13,10 @@ namespace Microsoft.AspNet.Mvc private IModelMetadataProvider _modelMetadataProvider; private IEnumerable _validatorProviders; private IEnumerable _valueProviderFactories; + private CompositeModelBinder _modelBinder; + private CompositeValueProvider _compositeValueProvider; + private bool _modelBinderInitialized; + private object _modelBinderInitLocker = new object(); public BaseController() { @@ -32,21 +37,25 @@ namespace Microsoft.AspNet.Mvc protected Task TryUpdateModelAsync(TModel model) { - var binder = new CompositeModelBinder(_modelBinders); - var requestContext = new RequestContext(Context, ActionContext.RouteValues); + LazyInitializer.EnsureInitialized(ref _modelBinder, ref _modelBinderInitialized, ref _modelBinderInitLocker, () => + { + var requestContext = new RequestContext(Context, ActionContext.RouteValues); + _compositeValueProvider = new CompositeValueProvider(_valueProviderFactories.Select(vpf => vpf.GetValueProvider(requestContext))); + return new CompositeModelBinder(_modelBinders); + }); + var bindingContext = new ModelBindingContext { MetadataProvider = _modelMetadataProvider, Model = model, ModelState = ModelState, ValidatorProviders = _validatorProviders, - ModelBinder = binder, + ModelBinder = _modelBinder, HttpContext = Context, - ValueProvider = new CompositeValueProvider(_valueProviderFactories.Select( - vpf => vpf.GetValueProviderAsync(requestContext).Result)) + ValueProvider = _compositeValueProvider }; - return binder.BindModelAsync(bindingContext); + return _modelBinder.BindModelAsync(bindingContext); } } } \ No newline at end of file diff --git a/src/MusicStore.Spa/Infrastructure/ForcedModelError.cs b/src/MusicStore.Spa/Infrastructure/ForcedModelError.cs index b4ae4a7518..0722550aa4 100644 --- a/src/MusicStore.Spa/Infrastructure/ForcedModelError.cs +++ b/src/MusicStore.Spa/Infrastructure/ForcedModelError.cs @@ -20,12 +20,10 @@ namespace System.ComponentModel.DataAnnotations public override bool IsValid(object value) { - return value == null || !value.Equals(FailValue); - // BUG: #ifdefs not working in editor #if DEBUG return value == null || !value.Equals(FailValue); #else - //return true; + return true; #endif } } diff --git a/src/MusicStore.Spa/MusicStore.Spa.kproj b/src/MusicStore.Spa/MusicStore.Spa.kproj index 86fa12ce07..9bc82fa3f9 100644 --- a/src/MusicStore.Spa/MusicStore.Spa.kproj +++ b/src/MusicStore.Spa/MusicStore.Spa.kproj @@ -163,15 +163,26 @@ + + + + + + + + + + + @@ -342,6 +353,7 @@ + @@ -610,39 +622,319 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -729,31 +1021,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1063,6 +1330,8 @@ + + @@ -1240,6 +1509,8 @@ + + @@ -1262,6 +1533,9 @@ + + + @@ -1311,15 +1585,172 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1327,14 +1758,13 @@ - - - - + + + @@ -1347,6 +1777,16 @@ + + + + + + + + + + @@ -1883,6 +2323,8 @@ + + @@ -1896,6 +2338,10 @@ + + + + @@ -2016,6 +2462,10 @@ + + + + @@ -2156,6 +2606,7 @@ + @@ -2326,6 +2777,11 @@ + + + + + @@ -2403,6 +2859,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2450,6 +2937,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2583,12 +3095,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2596,6 +3136,8 @@ + + @@ -2609,8 +3151,22 @@ + + + + + + + + + + + + + + @@ -2624,6 +3180,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2650,13 +3231,22 @@ + + + + + + + + + @@ -2670,6 +3260,16 @@ + + + + + + + + + + @@ -2707,16 +3307,34 @@ + + + + + + + + + + + + + + + + + + @@ -2733,6 +3351,12 @@ + + + + + + @@ -2870,9 +3494,22 @@ + + + + + + + + + + + + + @@ -2880,9 +3517,14 @@ + + + + + @@ -2979,8 +3621,8 @@ - + false true