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<TModel>` in one HTML helper - allows previous `ViewDataDictionary<TModel>` 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
This commit is contained in:
parent
a3b07dacdb
commit
e73655229c
|
|
@ -98,7 +98,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
{
|
||||
var metadata = ExpressionMetadataProvider.FromLambdaExpression<TModelItem, TValue>(
|
||||
expression,
|
||||
new ViewDataDictionary<TModelItem>(MetadataProvider),
|
||||
new ViewDataDictionary<TModelItem>(ViewData, model: null),
|
||||
MetadataProvider);
|
||||
|
||||
var expressionText = ExpressionHelper.GetExpressionText(expression);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -57,10 +57,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
typeof(ViewDataDictionary),
|
||||
(context) =>
|
||||
{
|
||||
return new ViewDataDictionary(context.ViewData)
|
||||
{
|
||||
Model = null
|
||||
};
|
||||
return new ViewDataDictionary(context.ViewData);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -65,12 +65,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
public ViewViewComponentResult View<TModel>(string viewName, TModel model)
|
||||
{
|
||||
var viewData = new ViewDataDictionary<TModel>(ViewData);
|
||||
if (model != null)
|
||||
{
|
||||
viewData.Model = model;
|
||||
}
|
||||
|
||||
var viewData = new ViewDataDictionary<TModel>(ViewData, model);
|
||||
return new ViewViewComponentResult(ViewEngine, viewName, viewData);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,12 +7,6 @@ namespace Microsoft.AspNet.Mvc
|
|||
{
|
||||
public class ViewDataDictionary<TModel> : ViewDataDictionary
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public ViewDataDictionary([NotNull] IModelMetadataProvider metadataProvider)
|
||||
: base(metadataProvider, declaredModelType: typeof(TModel))
|
||||
{
|
||||
}
|
||||
|
||||
// References may not show up due to ITypeActivator use in RazorPageActivator.
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ViewDataDictionary{TModel}"/> class.
|
||||
|
|
@ -75,6 +69,16 @@ namespace Microsoft.AspNet.Mvc
|
|||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ViewDataDictionary{TModel}"/> class.
|
||||
/// </summary>
|
||||
/// <remarks>Internal for testing.</remarks>
|
||||
/// <inheritdoc />
|
||||
internal ViewDataDictionary([NotNull] IModelMetadataProvider metadataProvider)
|
||||
: base(metadataProvider, declaredModelType: typeof(TModel))
|
||||
{
|
||||
}
|
||||
|
||||
public new TModel Model
|
||||
{
|
||||
get
|
||||
|
|
|
|||
|
|
@ -47,13 +47,14 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
// ViewContext.ViewData is an incompatibile type.
|
||||
if (context.ViewData == null)
|
||||
{
|
||||
// Create ViewDataDictionary<TModel>(metadataProvider);
|
||||
// Create ViewDataDictionary<TModel>(IModelMetadataProvider, ModelStateDictionary).
|
||||
return (ViewDataDictionary)_typeActivator.CreateInstance(context.HttpContext.RequestServices,
|
||||
activationInfo.ViewDataDictionaryType);
|
||||
activationInfo.ViewDataDictionaryType,
|
||||
context.ModelState);
|
||||
}
|
||||
else if (context.ViewData.GetType() != activationInfo.ViewDataDictionaryType)
|
||||
{
|
||||
// Create ViewDataDictionary<TModel>(ViewDataDictionary);
|
||||
// Create ViewDataDictionary<TModel>(ViewDataDictionary).
|
||||
return (ViewDataDictionary)_typeActivator.CreateInstance(context.HttpContext.RequestServices,
|
||||
activationInfo.ViewDataDictionaryType,
|
||||
context.ViewData);
|
||||
|
|
|
|||
Loading…
Reference in New Issue