ASP.NET MVC 2 Cookbook

ASP.NET MVC 2 Cookbook
eBook: $29.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 15%!
Print + free eBook + free PacktLib access to the book: $79.98    Print cover: $49.99
save 6%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • Solutions to the most common problems encountered with ASP.NET MVC development
  • Build and maintain large applications with ease using ASP.NET MVC
  • Recipes to enhance the look, feel, and user experience of your web applications
  • Expand your MVC toolbox with an introduction to lots of open source tools
  • Part of Packt's Cookbook series: Each recipe is a carefully organized sequence of instructions to complete the task as efficiently as possible

Book Details

Language : English
Paperback : 332 pages [ 235mm x 191mm ]
Release Date : January 2011
ISBN : 1849690308
ISBN 13 : 9781849690300
Author(s) : Andrew Siemer, Richard Kimber
Topics and Technologies : All Books, Application Development, Cookbooks, .NET, Open Source, Web Development

Table of Contents

Chapter 1: Working with the View
Chapter 2: Taking Action in Your Controllers
Chapter 3: Routing
Chapter 4: Master Pages
Chapter 5: Working with Data in the View
Chapter 6: Working with Forms
Chapter 7: Simplifying Complex Applications
Chapter 8: Validating MVC
Chapter 9: Data Access and Storage
Chapter 10: Application, Session, Cookies, and Caching
  • Chapter 1: Working with the View
    • Introduction
    • Using magic strings and the ViewData dictionary
    • Creating a strongly typed view
    • Decoupling a strongly typed view with a View model
    • Centralizing display logic with templated helpers
    • Using a partial view to segment view code
    • Rendering a child view with Html.RenderAction
    • Loading a view template from a database with NVelocity
    • Consuming a JSON with jQuery
    • Chapter 2: Taking Action in Your Controllers
      • Introduction
      • Exposing JSON using a JsonResult with Json.NET
      • A custom ActionResult to return an image
      • Specifying the size of an ImageResult
      • Creating a CAPTCHA system
      • Generating a PDF order summary
      • Implementing a controller factory for use with StructureMap
      • Chapter 3: Routing
        • Introduction
        • Creating a route to support a reporting engine
        • Making hackable URLs for a product catalog
        • Filter your matches with routing constraints
        • Using wildcard parameters to support slug URLs
        • Creating a 404 page via routing
        • Moving routes out of Global.asax
        • Supporting pagination in your URLs
        • Supporting content hierarchies with a custom RouteHandler
        • Creating a blacklist route constraint
        • Chapter 4: Master Pages
          • Introduction
          • How to create a master page
          • Determining the master page in the ActionResult
          • Controlling which master page is used with a view base class
          • Setting the master page from a controller base class
          • Passing data to the master page
          • Rendering data in a master page from another view
          • Creating nested master pages
          • Chapter 5: Working with Data in the View
            • Introduction
            • Reintroducing for and foreach
            • Handling an array of checkboxes
            • Handling an array of radio buttons
            • Working with a pageable set of data
            • How to navigate sortable data
            • Deleting a record with an intermediary "Are you sure?" page
            • Adding a jQuery delete link with confirmation
            • Master/detail page with inline details via jQuery and a partial view
            • Creating a master/detail page with modal pop-up and JSON
            • Chapter 6: Working with Forms
              • Introduction
              • Using HTML helpers to create forms
              • Building a custom HTML helper to display a WYSIWYG
              • Centralizing create and edit forms for reuse
              • Adding custom attributes to form elements
              • Defining a default button with jQuery
              • Hijaxing a form with jQuery
              • Performing an auto post-back with a select list
              • Autocomplete with jQuery UI
              • Chapter 7: Simplifying Complex Applications
                • Introduction
                • Centralized formatting of common types with templated helpers
                • Making templated helpers for custom types
                • Using areas to separate application components
                • Creating a "portable area" to use across multiple applications
                • Using input builders of MvcContrib
                • Generating forms with Html.InputForm()
                • Leaving breadcrumbs for your users with MvcSiteMap
                • Displaying tabular data in a grid
                • Chapter 8: Validating MVC
                  • Introduction
                  • Basic input validation
                  • Data annotations
                  • Client-side validation with jQuery
                  • Custom validators
                  • Remote validation with jQuery
                  • Chapter 9: Data Access and Storage
                    • Introduction
                    • Mocking your data layer with NBuilder
                    • Adding support for LINQ to SQL
                    • Going old school with ADO.NET
                    • XML documents as a data store
                    • Chapter 10: Application, Session, Cookies, and Caching
                      • Introduction
                      • Keeping track of anonymous visitors versus logged-in users
                      • Maintaining a user's information while at your site
                      • Remembering a previous visitor
                      • Caching your product catalog for faster response times
                      • Using output caching to cache an entire page
                      • Using partial views to cache pieces of a page individually
                      • Exposing an application API to your presentation layer

                      Andrew Siemer

                      Andrew Siemer is currently a Senior Technical Architect for Perficient in Austin, Texas. He has worked as a software engineer, architect, trainer, and author since 1998 when he left the military. He is a frequent contributor to, host of, author of ASP.NET 3.5 Social Networking, and a member of the ASP Insiders group. Andrew has provided consultancy to many companies on the topics of e-commerce, social networking, and various other business systems. He has worked with eUniverse, Point Vantage, Callaway Golf, Guidance Software, Intermix Media, Fox Interactive, Lamps Plus, and Lender Processing Services, to name a few. In addition to his daily duties, he also conducts classes in .NET, C#, and other web technologies, blogs on numerous topics (, and works on various community projects such as You can find Andrew Siemer on Twitter at @asiemer, as well as Facebook, LinkedIn, and various other social sites.

                      Richard Kimber

                      Richard Kimber has been working with web technologies for over 15 years. Primarily ASP.NET developer, Richard has worked in a broad range of development environments, from the financial services industry to new media and marketing. He now runs a web and mobile consultancy company called Dogma Creative with his wife Katie.

                      Code Downloads

                      Download the code and support files for this book.

                      Submit Errata

                      Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


                      - 2 submitted: last submission 09 Jul 2014

                      Page: 26 | Errata Type: Typo

                      Under bullet point 5, Views/Home/Address.ascx should be Views/Home/Address.aspx

                      Page: 32 | Errata Type: Technical


                      Should be Controllers/BlogController.cs: 


                      Should be Controllers/ProductController.cs: 


                      Sample chapters

                      You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

                      Frequently bought together

                      ASP.NET MVC 2 Cookbook +    Clojure Data Analysis Cookbook =
                      50% Off
                      the second eBook
                      Price for both: $43.05

                      Buy both these recommended eBooks together and get 50% off the cheapest eBook.

                      What you will learn from this book

                      • Get to grips with views and build view content from the data passed
                      • Work with actions and controllers to perform some important tasks such as creating a CAPTCHA system and generating a PDF order summary
                      • Get a handle on complex routing issues such as using wild card parameters to support slug URLs
                      • Create and use master pages to control application wide formatting
                      • Handle the creation of master-detail pages with AJAX modal pop-ups
                      • Use templated helpers, model binding, and areas to build large-scale applications with ease
                      • Perform various validations to ensure that the data entered is correct
                      • Access and store data with the different techniques available in ASP.NET MVC application development
                      • Manage the data at the application, site, caching, and cookie level
                      • Keep track of the number of visitors and maintain their information while they are at your site

                      In Detail

                      ASP.NET MVC, one of the latest web development platforms from Microsoft, brings the power of MVC programming to ASP.NET development. It simplifies the task of application development and maintenance for developers. However, ASP.NET MVC is filled with so many features that developers end up looking for solutions to the many problems that are encountered in their routine development tasks.ASP.NET MVC 2 Cookbook will provide solutions for the very specific problems that are encountered while developing applications with the ASP.NET MVC platform. It consists of many recipes containing step-by-step instructions that guide developers to effectively use the wide array of tools and features of ASP.NET MVC platform for web development

                      ASP.NET MVC Cookbook is a collection of recipes that will help you to perform your routine development tasks with ease using the ASP.NET MVC platform. In this book you will be walked through the solution to several specific web application development problems. Each recipe will walk you through the creation of a web application, setting up any Visual Studio project requirements, adding in any external tools, and finally the programming steps needed to solve the problem. The focus of the book is to describe the solution from start to finish.

                      The book starts off with recipes that demonstrate how to work effectively with views and controllers – two of the most important ingredients of the ASP.NET MVC framework. It then gradually moves on to cover many advanced routing techniques. Considering the importance of having a consistent structure to the site, the book contains recipes to show how to build a consistent UI and control its look with master pages. It also contains a chapter that is packed with many recipes that demonstrate how to gain control of data within a view.

                      As the book progresses through some exciting recipes on performing complex tasks with forms, you will discover how easy it is to work with forms to jazz up the look of your web site. Building large applications with ease is one of the prime features of the MVC model. Therefore, this book also focuses on tools and features that make building large applications easier to manage. As data plays an important role in the MVC architecture, there are ample recipes dedicated to cover data validation, access, and storage techniques. Finally, the book demonstrates how to enhance the user experience of your visitors by controlling the data at the application, session, caching, and cookie level.

                      By the end of this book, you will have explored a wide array of tools and features available with the ASP.NET MVC platform

                      A fast-paced cookbook with recipes covering all that you wanted to know about developing with ASP.NET MVC


                      Written in cookbook style, this book offers solutions to all common web development problems through recipes. Each recipe contains step-by-step instructions followed by the analysis of what was done in each task and other useful information. The book is designed so that you can read it chapter by chapter, or you can look at the list of recipes and refer to them in no particular order.

                      Who this book is for

                      This book is particularly written for web developers looking to transfer their knowledge from the ASP.NET Web Forms way of doing things to the ASP.NET MVC framework. As this book targets readers of various experience levels, you should be able to find recipes of a basic, intermediate, and advanced nature. Regardless of your experience level, each recipe will walk you through the solution in a step by step manner that anyone should be able to follow.

                      Code Download and Errata
                      Packt Anytime, Anywhere
                      Register Books
                      Print Upgrades
                      eBook Downloads
                      Video Support
                      Contact Us
                      Awards Voting Nominations Previous Winners
                      Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
                      Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software