in

Chennai .Net User Group

A platform that enables you to Learn, Share & Grow (India's first .Net user group)

Shiju Varghese's Blog

July 2008 - Posts

  • ASP.NET MVC Grid View using MVCContrib

    In this post, I demonstrate how you can use the  Grid UI helper of the MVCContrib project in your ASP.NET MVC  application. MVCContrib is a community project that adds the functionalities to Microsoft’s ASP.NET MVC Framework and makes the framework easier to use. MVCContrib provides several UI helpers and Grid UI helper is one of them. The Grid helper provides the functionalities of GridView control of ASP.NET GridView. The Grid component generates HTML tables for displaying data from a collection of Model objects and it support paging. The MVCContrib project can download from http://www.CodePlex.com/MvcContrib.

    The following are the steps to get Grid to work:

    Step1

    Add a reference to the MvcContrib assembly (download available from http://www.CodePlex.com/MvcContrib )

    Step 2

    Add a namespace import for MvcContrib.UI.Html to your web.config file:
    <pages>
        <namespaces>
            <add namespace="MvcContrib.UI"/>
            <add namespace="MvcContrib.UI.Html"/>
            <add namespace="MvcContrib.UI.Html.Grid"/>
           <add namespace="MvcContrib"/>
        </namespaces>
    </pages>

    Using the Grid

     The Grid supports pagination through the AsPagination extension method. For this, you should import the MvcContrib.Pagination namespace in your controller.

    Imports the following namespace to your controller class for pagination support

    using MvcContrib.Pagination;

     Controller Class
     

    public ActionResult List(int? page) {
        
    using (DBDataContext db = new DBDataContext()) {
         
    ViewData["categories"] = db.Categories.ToList().AsPagination(page ?? 1,10);
          
    return View("Categories");
        }

    The above action method of the Controller class used the AsPagination extension method. If you don't want pagination, you can  avoid  AsPagination method from Action method. The AsPagination extension method will work on any IEnumerable<T> or IQueryable<T>. This extension method has two arguments and the first one is the page number and the second optional parameter is the size of each page. The default value of the second parameter is 20. The pagination HTML will automatically generated by the Grid.

     View

    In your View you can then make a call to Html.Grid.

    div>
           
    <%
               
    Html.Grid<Category>(
                   
    "categories",
                   
    column => {
                       
    column.For(c => c.CategoryID, "ID");
                       
    column.For(c => c.CategoryName);
                       
    column.For(c => c.Description);
                       
    column.For("Edit").Do(c => { %>
                        
    <td><a href="/Category/Edit/<%= c.CategoryID %>">Edit</a>
                         
    </td>
                     
    <%}); }
              );
          
    %>    </div>

     
    The first parameter is the ViewData key that contains the Model data and the second parameter is used to construct the column model for the grid. The column.For will generates the columns for the Grid.The lambda expression c => c.CategoryID will generates the value of the property CategoryID and the value of the second parameter "ID" is the custom heading of the column. If custom heading is not specified, the name of the property will also use for heading. You can add custom columns by calling column.For("column name") and then specifying a lambda for generating the HTML. The last column.For method of the above view code will add a custom column name "Edit".

    For more details visit  http://www.CodePlex.com/MvcContrib

     

  • ASP.NET MVC Book List

    The below is the list of some of the upcoming books on ASP.NET MVC.  

    Pro ASP.NET MVC Framework (Pro)
    Author             :  Steve Sanderson
    Publisher         :  Apress
    Pub Date         :  August  2008

    Professional ASP.NET 3.5 MVC
    Author             :  Rob Conery , Scott Hanselman , Phil Haack       
    Publisher         :  Wrox
    Pub Date         :  October  2008

    ASP.NET MVC Framework Unleashed
    Author             :  Stephen Walther
    Publisher         :  Sams
    Pub Date         :  January  2009

    ASP.NET MVC in Action
    Author             :  Jeffrey Palermo, Dave Verwer, and Ben Scheirman
    Publisher         :  Manning
    Pub Date         :  January  2009



  • ASP.NET MVC Preview 4 Released

     ASP.NET MVC Preview 4  has been released on the CodePlex. You can download it from here.The Preview 4 release provides the new features such as OutputCache Action Filter, Authorize Action Filter, AccountController, Ajax Helpers and Namespaces in Routes.Visit Guru Gu's (Scott Guthrie) blog entry for more detailed informations. Now the ASP.NET MVC is getting the maturity for live production and the product is going close to the official beta, after that RTM version will be available. I hope the community can expect V 1.0 release on this year itself and new enhanced features will be included in a future full release.

    The following is the higher level roadmap of MVC that available from CodePlex.

    •  Improved Ajax Support
               -Support multiple partial updates
               -Improvements to the Ajax helper methods (link to action, ajax form, field and form observers)
    •  Helper Methods Improvements
    •  Validation Message Reporting

               - This is not the validation itself, but an underlying framework for reporting validation errors to the view
               - Includes validation message aware helper methods

    • Various other refactorings, bug fixes, and polish

     

     

    /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;}

  • ASP.net MVC Vs ASP.net Web Form

     
    Software Architects have been involving lot of debates about different approaches and architectures. Some of the examples are ORM Vs Store Procedures, REST Vs SOAP, etc. There is a debate happening inside the Microsoft community about ASP.net web form Vs ASP.net MVC. Many people thinking that ASP.net MVC will be replace webforms at least eventually and others are thinking that ASP.net MVC will not be replace webforms. Will ASP.net MVC replace webforms?. ASP.net MVC is an alternative approach to webforms rather than a replacement. It will not replace webforms and webforms will not replace ASP.NET MVC. The fact is that ASP.NET MVC and webforms will co-exist and that ASP.NET MVC is not a replacement for webforms. If you prefer ASP.net MVC use it and you feel webform is more comfortable, you can use it. . Both approaches are just choices and different approaches and choices are good things. Different choices are available for other platforms especially in the Java platform.

    Problems with ASP.net Web Form

    What are the problems with webforms? In webforms, Microsoft has tried to make windows form model development for web application development. That model was attracted lot of windows form developers especially VB 6.0 developers. Many of VB 6.0 developers had moved to ASP.net web development without knowing the basics of HTTP and web. For simulating windows form model development experience, webforms introduced event-driven approach and also introduced Viewstate and Postback. The end result is that web forms breaks the stateless nature of the Web. Both Viewstate and Postbacks have been made lot of problems and increased complexity of the web application development. Many web pages having hundreds of KB size of Viewstate that affected the performance of the applications sometime. Developers do not have the control of the rendering HTML of web forms and Server controls that render html with mixed inline style and deprecated tags that does not follows standards. Another problem with Web Forms is the integration of JavaScript frameworks due to the naming conventions of rendered HTML. The page life cycle of the Web Form is too complex and has the tightly coupling between all things in the ASP.net framework and a single class is used both to display output and handles user input. So unit testing is almost an impossible task. Today unit testing is very important in modern software development especially when we following agile methodologies and practices. Since web is a stateless thing, Events, Postbacks and Viewstate are not a good way. Today many ASP.net web form developers are facing different type pf browser compatibility issues when developing public face internet applications

    The ASP.net MVC way

    The ASP.NET MVC simplifies the complex parts of ASP.net Web Forms without any compromise of the power and flexibility of ASP.NET platform. ASP.net MVC implements Model-View-Controller UI pattern for web application development that lets you allows to develop applications in a loosely couples manner. MVC pattern is separating the application in three parts- Model, View and Controller. A view is responsible for rendering the user interface (UI) of the application and it is nothing more than html templates that filled with application’s data passed by the controller. The Model implements the logic for the application's data and it represents the business objects of the application that using the View for rendering user interface. Controllers are handles and responds to user input and interaction. The web request will be handled by the controller, and the controller will decide which model objects to use and which view objects to render. The MVC model replaces the Web Form events with the controller actions. The main advantages of the MVC models are clear separation of concerns, unit testing facility, and more control over the URLs and HTML. The MVC model does not use Viewstate, Postbacks, Server controls, and server-based forms that enable full control over the application and html rendered by the Views. MVC model is using Representational state transfer (REST) based URLs instead of file-name extensions used by the Web Form model so that we can make search engine optimization (SEO) URLs published by the application. Currently MVC supports only .Net Framework 3.5 and if you want to use with .Net Framework 2.0, visit Scott Hanselman’s blog entry Deploying ASP.NET MVC on ASP.NET 2.0

    The below code shows the implementation of MVC application.

    ProductsController.cs (Controller)

     


    In this sample, I have used extension methods to the HtmlHelper class to display ordered list of information.

    OrderListExtensions.cs


    Category.aspx (View) 

     

    Advantages of MVC Model

    1. Enable clean separation of concerns (SoC) .
    2. Enable full control over the rendered HTML.
    3. Enable Test Driven Development (TDD) (built with TDD in mind).
    4. SEO and REST friendly URL.
    5. Easy integration with JavaScript frameworks.
    6. Support third-party view engines such as NVelocity, Brail, NHaml.
    7. No ViewState and PostBack events.
    8. Follows the stateless nature of web.
    9. Extensible and Pluggable framework. 
    10. Ideal platform for Web 2.0 applications.

    Advantages of Web Form Model

    1. Provides RAD development.
    2. Easy development model for heavy data-driven LOB applications.
    3. Provides rich controls.
    4. Familiar model for windows form developers.

    Which is the best approach?

    The choice would be vary on different people. If you want more control over the HTML or you want Test Driven Development (TDD), or you care about web standards, accessibility, or you want to build SEO based URLs, you can choose MVC model. If you want rich controls and state oriented event-driven web development, you can choose Web Forms model. If you feel more comfortable with MVC, choose that model and you feel Web Form model is more comfortable, choose that model. Both are just choices. If you start your career with ASP.net Web Forms and do not have full knowledge of Web, it will be very difficult moving to MVC model.

    I prefer MVC over Web Forms and I feel that Microsoft is going to a right direction through MVC. Its technical features as well as the open source nature are attracted me a lot.The MVC model allows me full control over the HTML and enables Test Driven Development (TDD). We can easily integrate with jQuery and other JavaScript frameworks with MVC. Using extension methods of C# 3.0, we can make powerful and rich HTML helper methods. I believe that tesatbility, refactoring capability and maintainability are the main factors for a successful project and prefer these factors than RAD capability. The MVC model allows to build highly testable, maintainable loosely coupled applications with good practices such as TDD, Seperation of Concerns (SoC) and Dependency Injection (DI). You MUST use ASP.NET MVC for public face internet applications.

Copyright © 2002-2008 Chennai .Net User Group. All Rights Reserved. Microsoft and Microsoft logo's are trademarks of Microsoft Corporation