background preloader

HtmlHelper

Facebook Twitter

ASP.NET MVC 3 Custom HTML Helpers- Best Practices/Uses. Kiran Chand's Blog : Adding html attributes support for Templates - ASP.Net MVC 2.0 Beta. ASP.Net mvc 2.0 beta, supports templates feature. Templates are a way to write reusable view code. They significantly simplifies the code we need to write in views. You can read more about templates here. In this post I will show a way to extend the ModelMetadata and add support for Html Attributes which can be used in templates.

Let us assume there is a view model for "Employee" 1: public class Employee 3: public string Title { get; set; } 4: public string FirstName { get; set; } 5: public string LastName { get; set; } Here is the controller’s action to render the view model 1: public class HomeController : Controller 3: public ActionResult Index() 5: Employee employee = new Employee(); 6: return View(employee); And the view code <% using(Html.BeginForm()) <%=Html.EditorFor(e => e.Title)%> <%=Html.EditorFor(e => e.FirstName)%> <%=Html.EditorFor(e => e.LastName)%> Html for the Title field will be rendered as the following Default metadata for the view model won’t allow us to specify these attributes.

Using the TagBuilder Class to Build HTML Helpers. Stephen Walther introduces you to a useful utility class in the ASP.NET MVC framework named the TagBuilder class. You can use the TagBuilder class to easily build HTML tags. The ASP.NET MVC framework includes a useful utility class named the TagBuilder class that you can use when building HTML helpers. The TagBuilder class, as the name of the class suggests, enables you to easily build HTML tags. In this brief tutorial, you are provided with an overview of the TagBuilder class and you learn how to use this class when building a simple HTML helper that renders HTML <img> tags.

Overview of the TagBuilder Class The TagBuilder class is contained in the System.Web.Mvc namespace. AddCssClass() - Enables you to add a new class=”” attribute to a tag. The TagBuilder class has four important properties: Attributes - Represents all of the attributes of the tag. These methods and properties give you all of the basic methods and properties that you need to build up an HTML tag. Author Information. TagBuilder Class (System.Web.Mvc) Build a MVC web site HtmlHelper library and deliver it with NuGet - I Want My MVC. Posted on April 28th, 2011 If your workload includes crafting multiple web sites you know that there are several components that are commonly used in each site. Some of those common elements are Google's Analytics tracking JavaScript and meta tags used in the site submission process to help verify site ownership to both Google and Bing. It would be super ideal if we could try and find a way to have that code already written and be able to quickly add it to a new web site project built upon ASP.NET MVC3.

Lets make it happen. Building a common MVC web site library We will begin by creating a new Windows -> Class Library project named MvcWebsiteFramework. We can use this class library to add a whole bunch of commonly used code over time that will speed up our web site development. MVC has a class, System.Web.Mvc.HtmlHelper, that has been around since the beginning to help render common HTML content. Before we write our extension methods, lets go over the markup that we will need to render. <?