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>(
|
var metadata = ExpressionMetadataProvider.FromLambdaExpression<TModelItem, TValue>(
|
||||||
expression,
|
expression,
|
||||||
new ViewDataDictionary<TModelItem>(MetadataProvider),
|
new ViewDataDictionary<TModelItem>(ViewData, model: null),
|
||||||
MetadataProvider);
|
MetadataProvider);
|
||||||
|
|
||||||
var expressionText = ExpressionHelper.GetExpressionText(expression);
|
var expressionText = ExpressionHelper.GetExpressionText(expression);
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
var viewData = new ViewDataDictionary(_viewData)
|
var viewData = new ViewDataDictionary(_viewData, model: null)
|
||||||
{
|
{
|
||||||
Model = _metadata.Model,
|
Model = _metadata.Model,
|
||||||
ModelMetadata = _metadata
|
ModelMetadata = _metadata
|
||||||
|
|
|
||||||
|
|
@ -57,10 +57,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
typeof(ViewDataDictionary),
|
typeof(ViewDataDictionary),
|
||||||
(context) =>
|
(context) =>
|
||||||
{
|
{
|
||||||
return new ViewDataDictionary(context.ViewData)
|
return new ViewDataDictionary(context.ViewData);
|
||||||
{
|
|
||||||
Model = null
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -65,12 +65,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
public ViewViewComponentResult View<TModel>(string viewName, TModel model)
|
public ViewViewComponentResult View<TModel>(string viewName, TModel model)
|
||||||
{
|
{
|
||||||
var viewData = new ViewDataDictionary<TModel>(ViewData);
|
var viewData = new ViewDataDictionary<TModel>(ViewData, model);
|
||||||
if (model != null)
|
|
||||||
{
|
|
||||||
viewData.Model = model;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ViewViewComponentResult(ViewEngine, viewName, viewData);
|
return new ViewViewComponentResult(ViewEngine, viewName, viewData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,12 +7,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
public class ViewDataDictionary<TModel> : ViewDataDictionary
|
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.
|
// References may not show up due to ITypeActivator use in RazorPageActivator.
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ViewDataDictionary{TModel}"/> class.
|
/// 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
|
public new TModel Model
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
|
||||||
|
|
@ -47,13 +47,14 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
// ViewContext.ViewData is an incompatibile type.
|
// ViewContext.ViewData is an incompatibile type.
|
||||||
if (context.ViewData == null)
|
if (context.ViewData == null)
|
||||||
{
|
{
|
||||||
// Create ViewDataDictionary<TModel>(metadataProvider);
|
// Create ViewDataDictionary<TModel>(IModelMetadataProvider, ModelStateDictionary).
|
||||||
return (ViewDataDictionary)_typeActivator.CreateInstance(context.HttpContext.RequestServices,
|
return (ViewDataDictionary)_typeActivator.CreateInstance(context.HttpContext.RequestServices,
|
||||||
activationInfo.ViewDataDictionaryType);
|
activationInfo.ViewDataDictionaryType,
|
||||||
|
context.ModelState);
|
||||||
}
|
}
|
||||||
else if (context.ViewData.GetType() != activationInfo.ViewDataDictionaryType)
|
else if (context.ViewData.GetType() != activationInfo.ViewDataDictionaryType)
|
||||||
{
|
{
|
||||||
// Create ViewDataDictionary<TModel>(ViewDataDictionary);
|
// Create ViewDataDictionary<TModel>(ViewDataDictionary).
|
||||||
return (ViewDataDictionary)_typeActivator.CreateInstance(context.HttpContext.RequestServices,
|
return (ViewDataDictionary)_typeActivator.CreateInstance(context.HttpContext.RequestServices,
|
||||||
activationInfo.ViewDataDictionaryType,
|
activationInfo.ViewDataDictionaryType,
|
||||||
context.ViewData);
|
context.ViewData);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue