#@ template language="C#" HostSpecific="True" #> <#@ output extension="aspx" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Reflection" #> <# MvcTextTemplateHost mvcHost = (MvcTextTemplateHost)(Host); string mvcViewDataTypeGenericString = (!String.IsNullOrEmpty(mvcHost.ViewDataTypeName)) ? "<" + mvcHost.ViewDataTypeName + ">" : String.Empty; int CPHCounter = 1; #> <# // The following chained if-statement outputs the user-control needed for a partial view, or opens the asp:Content tag or html tags used in the case of a master page or regular view page if(mvcHost.IsPartialView) { #> <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<#= mvcViewDataTypeGenericString #>" %> <# } else if(mvcHost.IsContentPage) { #> <%@ Page Title="" Language="C#" MasterPageFile="<#= mvcHost.MasterPageFile #>" Inherits="System.Web.Mvc.ViewPage<#= mvcViewDataTypeGenericString #>" %> <# foreach(string cphid in mvcHost.ContentPlaceHolderIDs) { if(cphid.Equals("TitleContent", StringComparison.OrdinalIgnoreCase)) { #> <#= mvcHost.ViewName #> <# CPHCounter++; } } #> <#= mvcHost.ViewName #> <# } else { #> <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<#= mvcViewDataTypeGenericString #>" %> <#= mvcHost.ViewName #> <# } #> <# if(!String.IsNullOrEmpty(mvcViewDataTypeGenericString)) { Dictionary properties = new Dictionary(); FilterProperties(mvcHost.ViewDataType, properties); #> <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %> <% using (Html.BeginForm()) {%> Fields <# foreach(KeyValuePair property in properties) { #> <#= property.Key #>: <%= Html.TextBox("<#= property.Key #>", <#= property.Value #>) %> <%= Html.ValidationMessage("<#= property.Key #>", "*") %> <# } #> <% } %> <%=Html.ActionLink("Back to List", "Index") %> <# } #> <# // The following code closes the asp:Content tag used in the case of a master page and the body and html tags in the case of a regular view page #> <# if(mvcHost.IsContentPage) { #> <# foreach(string cphid in mvcHost.ContentPlaceHolderIDs) { if(!cphid.Equals("TitleContent", StringComparison.OrdinalIgnoreCase) && !cphid.Equals(mvcHost.PrimaryContentPlaceHolderID, StringComparison.OrdinalIgnoreCase)) { CPHCounter++; #> <# } } #> <# } else if(!mvcHost.IsPartialView && !mvcHost.IsContentPage) { #> <# } #> <#+ public void FilterProperties(Type type, Dictionary properties) { if(type != null) { PropertyInfo[] publicProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); foreach (PropertyInfo pi in publicProperties) { if (pi.GetIndexParameters().Length > 0) { continue; } Type currentPropertyType = pi.PropertyType; Type currentUnderlyingType = System.Nullable.GetUnderlyingType(currentPropertyType); if(currentUnderlyingType != null) { currentPropertyType = currentUnderlyingType; } if (IsBindableType(currentPropertyType) && pi.CanRead && pi.CanWrite) { if(currentPropertyType.Equals(typeof(double)) || currentPropertyType.Equals(typeof(decimal))) { properties.Add(pi.Name, "String.Format(\"{0:F}\", Model." + pi.Name + ")"); } else if(currentPropertyType.Equals(typeof(DateTime))) { properties.Add(pi.Name, "String.Format(\"{0:d}\", Model." + pi.Name + ")"); } else { properties.Add(pi.Name, "Model." + pi.Name); } } } } } public bool IsBindableType(Type type) { bool isBindable = false; if (type.IsPrimitive || type.Equals(typeof(string)) || type.Equals(typeof(DateTime)) || type.Equals(typeof(decimal)) || type.Equals(typeof(Guid)) || type.Equals(typeof(DateTimeOffset)) || type.Equals(typeof(TimeSpan))) { isBindable = true; } return isBindable; } #>
<#= property.Key #>: <%= Html.TextBox("<#= property.Key #>", <#= property.Value #>) %> <%= Html.ValidationMessage("<#= property.Key #>", "*") %>