MvcTemplates is a project devoted to creating a complete suite of Razor Editor and Display templates that utilize attributes, built in models, and jQuery. It is developed in C# and packages the templates into an easy to use dll.

MvcTemplates was developed so that your project can have consistent markup while not giving up the flexibility that writing the markup yourself provides.

Getting Started

Getting started is easy! Add references to MvcTemplates.dll and Commons.Web.Mvc.PrecompiledViews. Then add the following line to your global.asax Application_Start:

public class MvcApplication : System.Web.HttpApplication
{
        protected void Application_Start()
        {
            MvcTemplates.Templates.Configure(); //add this line here!
            
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
}

To view all of the attributes, models, and templates that come out of the box visit out documentation page!

Sample View Model

using System.Web.Mvc;
using MvcTemplates.Model;

namespace MvcTemplates.Client.Models
{
    public class ViewModelForDocumentation
    {
        [HtmlAttributes( new[] {"class"}, new[] {"random_class"} )]   
        [HelpMessage("You can completely customize html attributes by using the HtmlAttributes Attribute")]
        public string TextBox { get; set; }

        [FieldContainer(typeof(CustomFieldsetTagBuilder))]
        [Label(DoNotRender = true)] //the fieldset tag builder will add the label into the legend
        [HelpMessage("You can control field and label containers by using the FieldContainer or LabelContainer Attribute")]
        public string TextBoxWithCustomFieldContainer { get; set; }

        public class CustomFieldsetTagBuilder : TagBuilder
        {
            public CustomFieldsetTagBuilder() : base( "fieldset" )
            {
                var legend = new TagBuilder( "legend" );
                legend.SetInnerText( "Text Box with Custom Field Container!" );
                legend.Attributes.Add( "style", "padding-top:20px;" );
                this.InnerHtml = legend.ToString();
            }
        }

        [AutoComplete( "Autocomplete", "Home", Delay = 0)]
        [HelpMessage("Start typing the word Link")]
        public string AutoComplete { get; set; }
        
        [HelpMessage("A static Help Message")]        
        public string HelpMessage { get; set; }
                
        /// <summary>
        /// DropDown has 2 properties:
        /// public object Value { get; set; }
        /// public IEnumerable<SelectListItem> Options { get; set; }
        /// </summary>
        public DropDown DropDown { get; set; }

        [DropDown("SelectListItemsViewDataKey")] //uses ViewData["SelectListItemsViewDataKey"] for the Options of the DropDown
        public string DropDownOptionsFromViewData { get; set; }

        /// <summary>
        /// CheckBoxList has 2 properties:
        /// public object[] SelectedOptions { get; set; }
        /// public IEnumerable<SelectListItem> Options { get; set; }
        /// </summary>
        public CheckBoxList CheckBoxList { get; set; }

        /// <summary>
        /// RadioButtonList has 2 properties:
        /// public object Value { get; set; }
        /// public IEnumerable<SelectListItem> Options { get; set; }
        /// </summary>
        public RadioButtonList RadioButtonList { get; set; }

        [DisplayFor] //calls Html.Display( property ) instead of Html.Editor( property )
        public string DisplayForNoInput { get; set; }
        
        [DoNotRender] //same as ScaffoldColumn(false) but much more intuitive
        public string IgnoreThisField { get; set; }

        [Label( RenderAfterField = true, PutInFieldContainer = true, Text = "Check box with Label Rendered after the Field" )]
        public bool CheckBoxWithLabelOnRight { get; set; }

        [LabelAfterField("two labels")]
        [HelpMessage("This checkbox shows that you can have an additional label if you use the LabelAfterField Attribute")]
        public bool AnotherCheckBox { get; set; }

        public ViewModelForDocumentation()
        {
            TextBox = "hi i'm a text box with a random_class css class added";            
            DisplayForNoInput = "I called Html.DisplayFor() instead of EditorFor";
            
            DropDown = new DropDown
                           {
                               Value = "using a model",
                               Options = new SelectList( new[] {"thing3", "thing4", "using a model"}, "using a model" )
                           };
            
            CheckBoxList = new CheckBoxList
                               {
                                   SelectedOptions = new[]{ "thing3", "thing4" },
                                   Options = new SelectList( new[]{ "thing1", "thing2", "thing3", "thing4" } )
                               };

            RadioButtonList = new RadioButtonList
                                  {
                                      Value = "using a model",
                                      Options = new SelectList( new[] {"thing3", "thing4", "using a model"}, "using a model" )
                                  };            
        }
    }    
}

Last edited Feb 22, 2011 at 3:42 AM by brk6004, version 13