Add `TemplateInfo` property to `ViewDataDictionary`
Compared to legacy MVC: - correct usings and namespace - remove `GetFullHtmlFieldId` method; depends on `HtmlHelper` method that can't be static - String -> string - remove reference to a legacy bug - convert internal `VisitedObjects` property to public `AddVisited()` method Further cleanup - remove explicit backing fields for remaining properties - add copy constructor to replace code distributed around legacy MVC - don't "combine and trim" in `GetFullHtmlFieldName()`
This commit is contained in:
parent
ffb73a1ed8
commit
f60f14d537
|
|
@ -0,0 +1,67 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
|
{
|
||||||
|
public class TemplateInfo
|
||||||
|
{
|
||||||
|
// Keep a collection of visited objects to prevent infinite recursion.
|
||||||
|
private HashSet<object> _visitedObjects;
|
||||||
|
|
||||||
|
public TemplateInfo()
|
||||||
|
{
|
||||||
|
FormattedModelValue = string.Empty;
|
||||||
|
HtmlFieldPrefix = string.Empty;
|
||||||
|
_visitedObjects = new HashSet<object>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TemplateInfo(TemplateInfo original)
|
||||||
|
{
|
||||||
|
FormattedModelValue = original.FormattedModelValue;
|
||||||
|
HtmlFieldPrefix = original.HtmlFieldPrefix;
|
||||||
|
_visitedObjects = new HashSet<object>(original._visitedObjects);
|
||||||
|
}
|
||||||
|
|
||||||
|
public object FormattedModelValue { get; set; }
|
||||||
|
|
||||||
|
public string HtmlFieldPrefix { get; set; }
|
||||||
|
|
||||||
|
public int TemplateDepth
|
||||||
|
{
|
||||||
|
get { return _visitedObjects.Count; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool AddVisited(object value)
|
||||||
|
{
|
||||||
|
return _visitedObjects.Add(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetFullHtmlFieldName(string partialFieldName)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(partialFieldName))
|
||||||
|
{
|
||||||
|
return HtmlFieldPrefix;
|
||||||
|
}
|
||||||
|
else if (string.IsNullOrEmpty(HtmlFieldPrefix))
|
||||||
|
{
|
||||||
|
return partialFieldName;
|
||||||
|
}
|
||||||
|
else if (partialFieldName.StartsWith("[", StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
// The partialFieldName might represent an indexer access, in which case combining
|
||||||
|
// with a 'dot' would be invalid.
|
||||||
|
return HtmlFieldPrefix + partialFieldName;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return HtmlFieldPrefix + "." + partialFieldName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Visited(ModelMetadata metadata)
|
||||||
|
{
|
||||||
|
return _visitedObjects.Contains(metadata.Model ?? metadata.ModelType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -21,6 +21,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
[NotNull] ModelStateDictionary modelState)
|
[NotNull] ModelStateDictionary modelState)
|
||||||
{
|
{
|
||||||
ModelState = modelState;
|
ModelState = modelState;
|
||||||
|
TemplateInfo = new TemplateInfo();
|
||||||
_data = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
_data = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
||||||
_metadataProvider = metadataProvider;
|
_metadataProvider = metadataProvider;
|
||||||
}
|
}
|
||||||
|
|
@ -41,6 +42,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
: this(source.MetadataProvider)
|
: this(source.MetadataProvider)
|
||||||
{
|
{
|
||||||
_modelMetadata = source.ModelMetadata;
|
_modelMetadata = source.ModelMetadata;
|
||||||
|
TemplateInfo = new TemplateInfo(source.TemplateInfo);
|
||||||
|
|
||||||
foreach (var entry in source.ModelState)
|
foreach (var entry in source.ModelState)
|
||||||
{
|
{
|
||||||
|
|
@ -75,6 +77,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TemplateInfo TemplateInfo { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provider for subclasses that need it to override <see cref="ModelMetadata"/>.
|
/// Provider for subclasses that need it to override <see cref="ModelMetadata"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue