• English (United States)
    • العربية (مصر)
    • Deutsch (Deutschland)
    • Español (España, alfabetización internacional)
    • français (France)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • русский (Россия)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(中华人民共和国)
    • 中文(香港特別行政區)
DotNetAge - Mvc & jQuery CMS
Hide sidebar

Component Model for ASP.NET MVC3


Overview



In this article i will show you how to build a component model for ASP.NET MVC3 with Builder and Factory design patterns. You could use this model to write your strongly type components to binding data, handling hierarchical objects and write jQuery UI controls / Html5 controls for ASP.NET MVC3.

Background



As we know in classical ASP.NET we could write controls by using ASP.NET control model for our web applications. Today in MVC3 we’re usually write helpers instead of controls. I think helper methods is a good choice for some of simple use scenarios but not for all. For render single tag, img or scripts helper methods does well. I can help thing how to develop more complex controls with help methods ? How to write template container controls ? How to write a data bindable treeview and menu ? How to write and use that complex controls easily ? In fact i love the helper method and i hate them at the same time. Because helper methods is the easy way to output my tags on view, but that is not OOP way ! Helpers is easy to use but hard to extend and maintenance. How could you read, remember and maintenance a method with over 10 overload version ? That is hell! So we need another way out! The way that could easy to learn, more readable, OOP , extensible and familiar for our control developing.

Before you start



There are some concepts you need to know:


Objectives



We need a component model for ASP.NET MVC
  • Supports Razor and Web Form syntax.
  • Offers interfaces or abstract classes to implement.
  • Supports control template.
  • Supports composite components.
  • Supports data binding.
  • Easy to integrate with client ability (javascripts jQuery).

Usage Design



The biggest difference between Mvc3 and classic ASP.NET is Mvc3 using Views and helpers to render html tags directly but no any component model like classic ASP.NET. So before design the component model we need to know how many ways could write controls in Mvc3 then we also need to know the common behaviors, properties, usages of the controls. Now let’s look back in Mvc3 and do some testing.

Usage1:Extension method



We calls “Html.XXX()” and “Ajax.XXX()” Mvc Helpers or Extensions,actually they are static extension method that is a good way to inject new methods to an exists class. When you are using Razor it provides a new syntax to write the helper method by using @helper. I put build in helpers which we are often use in the following list,(notes:each method has more then 5 overload versions.):

  • Html.Display()
  • Html.DisplayFor()
  • Html.Label()
  • Html.LabelFor()
  • Html.LablelForModel()
  • Html.ActionLink()
  • Html.RoutLink()
  • Html.Editor()
  • Html.EditorFor()
  • Html.EditorForModel()
  • Html.CheckBox()
  • Html.CheckBoxFor()
  • Html.Hidden()
  • Html.Password()
  • Html.PasswordFor()
  • Html.RadioButton()
  • Html.RadioButtonFor()
  • Html.TextBox()
  • Html.TextBoxFor()
  • Html.ListBox()
  • Html.ListBoxFor()
  • Html.DropDownList()
  • Html.DropDownListFor()
  • Html.TextArea()
  • Html.TextAreaFor()




There are almost over 100 methods with overload versions of this list! Please note that these helpers only use to render
Share to:

Tag cloud

Anything in here will be replaced on browsers that support the canvas element