From e73655229c41ce651fdcb3b7553d26aa719f1957 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Sat, 25 Oct 2014 15:08:56 -0700 Subject: [PATCH] Correct some `ViewDataDictionary` constructor calls - ensure correct `ViewData.ModelState` value in a `RazorPage` instance - let view components inherit `ViewData.Model` from surrounding context - do not create an isolated `ViewDataDictionary` in one HTML helper - allows previous `ViewDataDictionary` constructor to be `internal` - also add comments for that constructor - change two `ViewDataDictionary` copy constructor calls to avoid uselessly copying `Model` and related information from source --- .../Rendering/Html/HtmlHelperOfT.cs | 2 +- .../Rendering/Html/TemplateBuilder.cs | 2 +- .../DefaultViewComponentActivator.cs | 5 +---- .../ViewComponents/ViewComponent.cs | 7 +------ .../ViewDataDictionaryOfT.cs | 16 ++++++++++------ .../RazorPageActivator.cs | 7 ++++--- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs index cc6124201e..3dc5a53e13 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs @@ -98,7 +98,7 @@ namespace Microsoft.AspNet.Mvc.Rendering { var metadata = ExpressionMetadataProvider.FromLambdaExpression( expression, - new ViewDataDictionary(MetadataProvider), + new ViewDataDictionary(ViewData, model: null), MetadataProvider); var expressionText = ExpressionHelper.GetExpressionText(expression); diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/TemplateBuilder.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/TemplateBuilder.cs index f354bf9127..418a02bb92 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/TemplateBuilder.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/TemplateBuilder.cs @@ -63,7 +63,7 @@ namespace Microsoft.AspNet.Mvc.Rendering return string.Empty; } - var viewData = new ViewDataDictionary(_viewData) + var viewData = new ViewDataDictionary(_viewData, model: null) { Model = _metadata.Model, ModelMetadata = _metadata diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentActivator.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentActivator.cs index 67e8c9f728..873dfb1c3d 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentActivator.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentActivator.cs @@ -57,10 +57,7 @@ namespace Microsoft.AspNet.Mvc typeof(ViewDataDictionary), (context) => { - return new ViewDataDictionary(context.ViewData) - { - Model = null - }; + return new ViewDataDictionary(context.ViewData); } } }; diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs index 5ae35e94ab..01cdbabeb5 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs @@ -65,12 +65,7 @@ namespace Microsoft.AspNet.Mvc public ViewViewComponentResult View(string viewName, TModel model) { - var viewData = new ViewDataDictionary(ViewData); - if (model != null) - { - viewData.Model = model; - } - + var viewData = new ViewDataDictionary(ViewData, model); return new ViewViewComponentResult(ViewEngine, viewName, viewData); } } diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewDataDictionaryOfT.cs b/src/Microsoft.AspNet.Mvc.Core/ViewDataDictionaryOfT.cs index 8df9997a18..a4c063f3dd 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewDataDictionaryOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewDataDictionaryOfT.cs @@ -7,12 +7,6 @@ namespace Microsoft.AspNet.Mvc { public class ViewDataDictionary : ViewDataDictionary { - /// - public ViewDataDictionary([NotNull] IModelMetadataProvider metadataProvider) - : base(metadataProvider, declaredModelType: typeof(TModel)) - { - } - // References may not show up due to ITypeActivator use in RazorPageActivator. /// /// Initializes a new instance of the class. @@ -75,6 +69,16 @@ namespace Microsoft.AspNet.Mvc { } + /// + /// Initializes a new instance of the class. + /// + /// Internal for testing. + /// + internal ViewDataDictionary([NotNull] IModelMetadataProvider metadataProvider) + : base(metadataProvider, declaredModelType: typeof(TModel)) + { + } + public new TModel Model { get diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorPageActivator.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorPageActivator.cs index 74dd98d87f..aa4f99dd48 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorPageActivator.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorPageActivator.cs @@ -47,13 +47,14 @@ namespace Microsoft.AspNet.Mvc.Razor // ViewContext.ViewData is an incompatibile type. if (context.ViewData == null) { - // Create ViewDataDictionary(metadataProvider); + // Create ViewDataDictionary(IModelMetadataProvider, ModelStateDictionary). return (ViewDataDictionary)_typeActivator.CreateInstance(context.HttpContext.RequestServices, - activationInfo.ViewDataDictionaryType); + activationInfo.ViewDataDictionaryType, + context.ModelState); } else if (context.ViewData.GetType() != activationInfo.ViewDataDictionaryType) { - // Create ViewDataDictionary(ViewDataDictionary); + // Create ViewDataDictionary(ViewDataDictionary). return (ViewDataDictionary)_typeActivator.CreateInstance(context.HttpContext.RequestServices, activationInfo.ViewDataDictionaryType, context.ViewData);