1 namespace System.Web.Mvc.Html {
3 using System.Collections;
4 using System.Collections.Generic;
6 using System.Data.Linq;
7 using System.Globalization;
10 using System.Web.Mvc.Resources;
11 using System.Web.UI.WebControls;
13 internal static class DefaultEditorTemplates {
14 internal static string BooleanTemplate(HtmlHelper html) {
16 if (html.ViewContext.ViewData.Model != null) {
17 value = Convert.ToBoolean(html.ViewContext.ViewData.Model, CultureInfo.InvariantCulture);
20 return html.ViewContext.ViewData.ModelMetadata.IsNullableValueType
21 ? BooleanTemplateDropDownList(html, value)
22 : BooleanTemplateCheckbox(html, value ?? false);
25 private static string BooleanTemplateCheckbox(HtmlHelper html, bool value) {
26 return html.CheckBox(String.Empty, value, CreateHtmlAttributes("check-box")).ToHtmlString();
29 private static string BooleanTemplateDropDownList(HtmlHelper html, bool? value) {
30 return html.DropDownList(String.Empty, TriStateValues(value), CreateHtmlAttributes("list-box tri-state")).ToHtmlString();
34 internal static string CollectionTemplate(HtmlHelper html) {
35 return CollectionTemplate(html, TemplateHelpers.TemplateHelper);
38 internal static string CollectionTemplate(HtmlHelper html, TemplateHelpers.TemplateHelperDelegate templateHelper) {
39 object model = html.ViewContext.ViewData.ModelMetadata.Model;
44 IEnumerable collection = model as IEnumerable;
45 if (collection == null) {
46 throw new InvalidOperationException(
48 CultureInfo.CurrentCulture,
49 MvcResources.Templates_TypeMustImplementIEnumerable,
50 model.GetType().FullName
55 Type typeInCollection = typeof(string);
56 Type genericEnumerableType = TypeHelpers.ExtractGenericInterface(collection.GetType(), typeof(IEnumerable<>));
57 if (genericEnumerableType != null) {
58 typeInCollection = genericEnumerableType.GetGenericArguments()[0];
60 bool typeInCollectionIsNullableValueType = TypeHelpers.IsNullableValueType(typeInCollection);
62 string oldPrefix = html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix;
65 html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = String.Empty;
67 string fieldNameBase = oldPrefix;
68 StringBuilder result = new StringBuilder();
71 foreach (object item in collection) {
72 Type itemType = typeInCollection;
73 if (item != null && !typeInCollectionIsNullableValueType) {
74 itemType = item.GetType();
76 ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(() => item, itemType);
77 string fieldName = String.Format(CultureInfo.InvariantCulture, "{0}[{1}]", fieldNameBase, index++);
78 string output = templateHelper(html, metadata, fieldName, null /* templateName */, DataBoundControlMode.Edit, null /* additionalViewData */);
79 result.Append(output);
82 return result.ToString();
85 html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = oldPrefix;
89 internal static string DecimalTemplate(HtmlHelper html) {
90 if (html.ViewContext.ViewData.TemplateInfo.FormattedModelValue == html.ViewContext.ViewData.ModelMetadata.Model) {
91 html.ViewContext.ViewData.TemplateInfo.FormattedModelValue = String.Format(CultureInfo.CurrentCulture, "{0:0.00}", html.ViewContext.ViewData.ModelMetadata.Model);
94 return StringTemplate(html);
97 internal static string HiddenInputTemplate(HtmlHelper html) {
100 if (html.ViewContext.ViewData.ModelMetadata.HideSurroundingHtml) {
101 result = String.Empty;
104 result = DefaultDisplayTemplates.StringTemplate(html);
107 object model = html.ViewContext.ViewData.Model;
109 Binary modelAsBinary = model as Binary;
110 if (modelAsBinary != null) {
111 model = Convert.ToBase64String(modelAsBinary.ToArray());
114 byte[] modelAsByteArray = model as byte[];
115 if (modelAsByteArray != null) {
116 model = Convert.ToBase64String(modelAsByteArray);
120 result += html.Hidden(String.Empty, model).ToHtmlString();
124 internal static string MultilineTextTemplate(HtmlHelper html) {
125 return html.TextArea(String.Empty,
126 html.ViewContext.ViewData.TemplateInfo.FormattedModelValue.ToString(),
127 0 /* rows */, 0 /* columns */,
128 CreateHtmlAttributes("text-box multi-line")).ToHtmlString();
131 private static IDictionary<string, object> CreateHtmlAttributes(string className) {
132 return new Dictionary<string, object>() {
133 { "class", className }
137 internal static string ObjectTemplate(HtmlHelper html) {
138 return ObjectTemplate(html, TemplateHelpers.TemplateHelper);
141 internal static string ObjectTemplate(HtmlHelper html, TemplateHelpers.TemplateHelperDelegate templateHelper) {
142 ViewDataDictionary viewData = html.ViewContext.ViewData;
143 TemplateInfo templateInfo = viewData.TemplateInfo;
144 ModelMetadata modelMetadata = viewData.ModelMetadata;
145 StringBuilder builder = new StringBuilder();
147 if (templateInfo.TemplateDepth > 1) { // DDB #224751
148 return modelMetadata.Model == null ? modelMetadata.NullDisplayText : modelMetadata.SimpleDisplayText;
151 foreach (ModelMetadata propertyMetadata in modelMetadata.Properties.Where(pm => ShouldShow(pm, templateInfo))) {
152 if (!propertyMetadata.HideSurroundingHtml) {
153 string label = LabelExtensions.LabelHelper(html, propertyMetadata, propertyMetadata.PropertyName).ToHtmlString();
154 if (!String.IsNullOrEmpty(label)) {
155 builder.AppendFormat(CultureInfo.InvariantCulture, "<div class=\"editor-label\">{0}</div>\r\n", label);
158 builder.Append("<div class=\"editor-field\">");
161 builder.Append(templateHelper(html, propertyMetadata, propertyMetadata.PropertyName, null /* templateName */, DataBoundControlMode.Edit, null /* additionalViewData */));
163 if (!propertyMetadata.HideSurroundingHtml) {
165 builder.Append(html.ValidationMessage(propertyMetadata.PropertyName));
166 builder.Append("</div>\r\n");
170 return builder.ToString();
173 internal static string PasswordTemplate(HtmlHelper html) {
174 return html.Password(String.Empty,
175 html.ViewContext.ViewData.TemplateInfo.FormattedModelValue,
176 CreateHtmlAttributes("text-box single-line password")).ToHtmlString();
179 private static bool ShouldShow(ModelMetadata metadata, TemplateInfo templateInfo) {
183 && metadata.ModelType != typeof(EntityState)
185 && !metadata.IsComplexType
186 && !templateInfo.Visited(metadata);
189 internal static string StringTemplate(HtmlHelper html) {
190 return html.TextBox(String.Empty,
191 html.ViewContext.ViewData.TemplateInfo.FormattedModelValue,
192 CreateHtmlAttributes("text-box single-line")).ToHtmlString();
195 internal static List<SelectListItem> TriStateValues(bool? value) {
196 return new List<SelectListItem> {
197 new SelectListItem { Text = MvcResources.Common_TriState_NotSet, Value = String.Empty, Selected = !value.HasValue },
198 new SelectListItem { Text = MvcResources.Common_TriState_True, Value = "true", Selected = value.HasValue && value.Value },
199 new SelectListItem { Text = MvcResources.Common_TriState_False, Value = "false", Selected = value.HasValue && !value.Value },