Infragistics JQuery controls

ASP.NET MVC Basics: How to create a HtmlHelper

 ASP.NET MVC Basics: How to create a HtmlHelper Hello! And welcome to another tutorial on  This is the first tutorial in a series on ASP.NET MVC development.  This first installment will cover how to create an HtmlHelper.   What is ASP.NET MVC? I apologize if I'm stating the obvious, however, this is a tutorial on the basics so I'm approaching the tutorial with the assumption that the reader has no knowledge of ASP.NET, MVC, or ASP.NET MVC.  I'm sure you noticed that I made three references to the technology.  That's because ASP.NET MVC is more than one technology.  First we have ASP.NET, which is a server side web development technology created by Microsoft that utilizes the .NET framework.  Simply put, ASP.NET allows you to write dynamic web pages in .NET language of your choice (C#, VB.NET, etc..).  ASP.NET is an open source technology and it can be developed using the .NET framework or the Mono runtime.  You can find all sorts of wonderful information at . ASP.NET comes in two "flavors".  ASP.NET WebForms which is the original flavor, and ASP.NET MVC.  ASP.NET MVC is made up of two concepts.  First and foremost there is the MVC design pattern.  The MVC or Model-View-Controller design pattern was introduced by Trygve Reenskaug in the 1970's.  The design pattern has also be explained by Martin Fowler in his wonderful book on design patterns called Patterns of Enterprise Application Architecture . The idea behind the MVC pattern is that you have data that you wish to display (Model), you have a presentation layer in which you wish to display your Model (View), and you have a class that handles the interaction between the View and the Model (controller).  The MVC design pattern is an excellent example of a design with proper separation of concerns.  Applications that implement the MVC design pattern are generally loosely coupled, and easy to test (unit testing).  In ASP.NET MVC, the View is a web page, the model can be any class that holds data that you want to display in the View, and a controller class. Now that I've explained ASP.NET and MVC, I'll now explain ASP.NET MVC.  You see, not only is ASP.NET MVC an implementation of the Model-View-Controller design pattern, it's also a framework built by Microsoft to support the implementation of the MVC design pattern.  You will see as you begin working with MVC that the framework is a large part of what makes ASP.NET MVC what it is.  You could implement ASP.NET MVC without the framwork, however, I don't recommend it.  The framework will create controller's for you, create Views that are bound to a strongly typed model of your choice, and much more.  Using the ASP.NET MVC framework, you can create a basic CRUD web application in a very short amount of time. I hope that this gives you a nice overview of the ASP.NET MVC framework and design pattern. HtmlHelper: What they are and how to create them I'll explain HtmlHelpers by taking you through implementing one..  We'll start by firing up Visual Studio and create a new ASP.NET MVC 4 project (note: I'm using VS 2012, however, if you have an earlier version installed, simply choose whichever ASP.NET MVC version that you have available to you.  If you don't have any ASP.NET MVC templates, you can use the Web platform installer to install ASP.NET MVC on your system.       The next dialog allows you to choose which type of ASP.NET MVC application that you'd like to create.  We will pick an Internet Application, with the Razor View engine and a unit testing project as shown below.     Now click OK and your project will be created for you.  The ASP.NET MVC Framework will create a lot of boiler plate code behind the scenes.  This includes "Forms based Authentication" in the web.config file as well as an AccountController which is a controller to handle authentication to the site.  There is also a default controller called HomeController. We are going to create an html helper that creates an HTML table.  This is merely an example and is not particularly useful, however, it will show you how to create and use an HTML Helper.  An HTML helper is nothing more than an extension method which returns a string of HTML.  Here is the official definition of an extension method from MSDN: Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type. Creating the HtmlHelper  Create a folder called "Helpers" in your solution.  In the folder, create a static class called TableExtensions.cs .  The class will look like this: TableExtensions.cs using System; using System.Text; using System.Web.Mvc; namespace HtmlHelpers.Helpers { public static class TableExtensions { public static MvcHtmlString Table(this HtmlHelper helper, string id, string name, int rows, int columns) { if (string.IsNullOrEmpty(id)) throw new ArgumentNullException("id"); if(string.IsNullOrEmpty("name")) throw new ArgumentNullException("name"); if (rows <= 0) throw new IndexOutOfRangeException(); if (columns <= 0) throw new IndexOutOfRangeException(); StringBuilder tableBuilder = new StringBuilder(); tableBuilder.Append(string.Format("<table id=\"{0}\" name=\"{1}\">", id, name)); for (int rowCounter = 0; rowCounter < rows; rowCounter++) { tableBuilder.Append("<tr>"); for (int columnCounter = 0; columnCounter < columns; columnCounter++) { tableBuilder.Append("<td>"); tableBuilder.Append(rowCounter.ToString()); tableBuilder.Append("</td>"); } tableBuilder.Append("</tr>"); } tableBuilder.Append("</table>"); return new MvcHtmlString(tableBuilder.ToString()); } } } As you can see, we've created a static class and a static method. The the first method parameter is  this HtmlHelper helperThis is where the magic happens. This allows us to use the method from the View like all other HTML helpers.The other methods specify the number of rows and columns that should be built in the HTML table.Here is an example of how to use the helper: @using HtmlHelpers.Helpers; @{ ViewBag.Title = "Index"; } <h2>Index</h2> @Html.Table("myTable", "myTableName", 4, 4)The output looks like this: And there you have it folks.  I hope you enjoyed this tutorial.  If you have any questions please don't hesitate to leave a comment. Thanks for reading.

Prism 4.1 release for .NET 4.5 and Windows 8

This is a short post but I'm happy to let you know that the Microsoft patterns and practices team are currently testing a new release of Prism, the composite user interface framework.  There are plans for the new release to support Windows 8 WinRT applications as well as .NET applications. I'm happy regarding this release because I was worried that this project may have been abandoned like Silverlight.   Read about it here.   Thanks   kick it on

Windows 8 sales are below Microsoft's internal projections

Windows 8 sales aren't meeting expectations I'd like to first start by saying that I am a fan of Windows 8.  I'm a software developer so naturally I purchased a tablet and installed Windows 8 professional so I would have something to test on.  I find that I use it quite often.  The fact remains that I can see where the average user who cares nothing about tablets or touch screen would have problems with the new OS.  The Windows 8 strategy by Microsoft has seemed awkward and rushed from the beginning.  WinRT has been a mystery to most users, and the lawsuits surrounding the Metro terminology doesn't help matters at all.  I personally think that it would have made sense for Microsoft to enhance Windows 7 to give the user's a reason to upgrade to a new OS and then release WinRT/Metro as a new tablet platform.  It seems Microsoft decided to combine the tablet and PC operating systems to use their weight to force a move toward touch screens and tablets. has reported that a source close to Microsoft has reported that Windows 8 sales are "below the companies internal projections".  Will Microsoft listen to it's user's and create a new OS?  Basically a situation like we had with Vista and Windows 7?  Only time will tell. Here is the article on Windows 8 PC sales reportedly 'well below Microsoft's internal projections'  What are you thoughts on the subject?  What can Microsoft do to turn things around?  What do you think about Windows 8?  Share your comments! kick it on    

Syncfusion Essential Studio for WinRT Beta and Metro Studio product reviews

Syncfusion Essential Studio for WinRT Beta Syncfusion has been a major player in the 3rd party development component market for a while now.  Their products include .NET development UI controls for ASP.NET, ASP.NET MVC, ASP.NET Mobile, WinForms, WPF, Silverlight, and more.  The newest tool release is a suite of controls that are made for Windows 8 WinRT development.  The product is called Essential Studio for WinRT and it's made up of a plethora of highly attractive, cutting edge controls that are specifically designed for WinRT and touch interfaces.   20 Highly optimized controls Syncfusion Essential Studio for WinRT comes with 20 control that are designed with touch control and performance in mind.  The controls are vast and provide everything that you need to developing cutting edge user interfaces for your Windows 8 applications such as dashboards, maps, charts, and more. Microsoft Office document creation from within your WinRT applications The Studio suite comes with controls that allow your application to create Microsoft documents like Word, Excel as well as Adobe PDF documents on the fly.  The controls are completely independent and do not require Microsoft Office or Adobe Acrobat to be installed on the machine.  This is an incredible value which will allow applications to create receipts, invoices, work orders, or any other sort of document that you'd find in a standard line of business application. Samples included As with their other products, Syncfusion has bundled many sample applications (with source code) to help you to get up and running with the tools in no time.  The samples are written in C#/VB.NET and XAML.  Creating Windows 8 Graphics with Metro Studio Syncfusion has also offered a free product called Syncfusion Metro Studio.  Metro Studio is an extremely useful application that provides a huge selection of "Metro" style art snippets that you can edit.  This is a tremendous solution that helps you to add graphics to you Windows 8 applications.  The best part is it's FREE! The artwork is divided into logical groups for you to choose the art that best suites your needs.   When you select a graphic, you are presented with the option to edit the image.  Here you can change the size and color.  You can also preform transformations like rotate.  Syncfusion Metro Studio allows you to output the edited image as standard image files as well as XAML.   As you can see, Syncfusion have provided two extremely useful tools for Windows 8 developers and not only are they first class tools, they are also free! Check them out today! Syncfusion Essential Studio for WinRT Beta  Syncfusion Metro Studio

About the author

My name is Buddy James.  I'm a Microsoft Certified Solutions Developer from the Nashville, TN area.  I'm a Software Engineer, an author, a blogger (, a mentor, a thought leader, a technologist, a data scientist, and a husband.  I enjoy working with design patterns, data mining, c#, WPF, Silverlight, WinRT, XAML, ASP.NET, python, CouchDB, RavenDB, Hadoop, Android(MonoDroid), iOS (MonoTouch), and Machine Learning. I love technology and I love to develop software, collect data, analyze the data, and learn from the data.  When I'm not coding,  I'm determined to make a difference in the world by using data and machine learning techniques. (follow me at @budbjames).  

Related links

Month List