MVC3 integrated, with some changes to make it compile on Mono and with Razor2
[mono.git] / mcs / class / System.Web.Mvc3 / Mvc / TemplateInfo.cs
1 namespace System.Web.Mvc {
2     using System.Collections.Generic;
3
4     public class TemplateInfo {
5         private string _htmlFieldPrefix;
6         private object _formattedModelValue;
7         private HashSet<object> _visitedObjects;
8
9         public object FormattedModelValue {
10             get {
11                 return _formattedModelValue ?? String.Empty;
12             }
13             set {
14                 _formattedModelValue = value;
15             }
16         }
17
18         public string HtmlFieldPrefix {
19             get {
20                 return _htmlFieldPrefix ?? String.Empty;
21             }
22             set {
23                 _htmlFieldPrefix = value;
24             }
25         }
26
27         public int TemplateDepth {
28             get {
29                 return VisitedObjects.Count;
30             }
31         }
32
33         // DDB #224750 - Keep a collection of visited objects to prevent infinite recursion
34         internal HashSet<object> VisitedObjects {
35             get {
36                 if (_visitedObjects == null) {
37                     _visitedObjects = new HashSet<object>();
38                 }
39                 return _visitedObjects;
40             }
41             set {
42                 _visitedObjects = value;
43             }
44         }
45
46         public string GetFullHtmlFieldId(string partialFieldName) {
47             return HtmlHelper.GenerateIdFromName(GetFullHtmlFieldName(partialFieldName));
48         }
49
50         public string GetFullHtmlFieldName(string partialFieldName) {
51             // This uses "combine and trim" because either or both of these values might be empty
52             return (HtmlFieldPrefix + "." + (partialFieldName ?? String.Empty)).Trim('.');
53         }
54
55         public bool Visited(ModelMetadata metadata) {
56             return VisitedObjects.Contains(metadata.Model ?? metadata.ModelType);
57         }
58     }
59 }