Professional ASP.NET MVC 1.0
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com.
Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana
Chapter 1 is licensed under the terms of the Creative Commons Attribution No Derivatives 3.0 license.
Published simultaneously in Canada
ISBN: 978-0-470-38461-9
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
Library of Congress Cataloging-in-Publication Data is available from the publisher.
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form
or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as
permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior
written permission of the Publisher, or authorization through payment of the appropriate per-copy
fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax
(978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions
Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax
(201) 748-6008, or online at
http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or war-
ranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim
all warranties, including without limitation warranties of fitness for a particular purpose. No warranty
may be created or extended by sales or promotional materials. The advice and strategies contained herein
may not be suitable for every situation. This work is sold with the understanding that the publisher is not
engaged in rendering legal, accounting, or other professional services. If professional assistance is required,
the services of a competent professional person should be sought. Neither the publisher nor the author
shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this
work as a citation and/or a potential source of further information does not mean that the author or the
publisher endorses the information the organization or Website may provide or recommendations it may
make. Further, readers should be aware that Internet Websites listed in this work may have changed or dis-
appeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department
within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related
trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the
United States and other countries, and may not be used without written permission. All other trademarks
are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or
vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not
be available in electronic books.
84619ffirsw.indd 6 3/3/09 8:30:50 AM
Professional ASP.NET MVC 1.0
Table of Contents
Chapter 1: NerdDinner.
Chapter 2: Model View Controller and ASP.NET.
Chapter 3: ASP.NET > ASP.NET MVC.
Chapter 4: Routes and URLs.
Chapter 5: Controllers.
Chapter 6: Views.
Chapter 7: AJAX.
Chapter 8: Filters.
Chapter 9: Securing Your Application.
Chapter 10: Test Driven Development With ASP.NET MVC.
Chapter 11: Testable Design Patterns.
Chapter 12: Best of Both Worlds: Web Forms and MVC Together.
Page 2 of 186
Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the
following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may
be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The
complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”
Chapter Contents
NerdDinner ............................................................................................................ 3
File->New Project ................................................................................................... 8
Creating the Database ......................................................................................... 17
Building the Model .............................................................................................. 26
Controllers and Views .......................................................................................... 42
Create, Update, Delete Form Scenarios ............................................................... 67
ViewData and ViewModel ................................................................................. 101
Partials and Master Pages .................................................................................. 108
Paging Support ................................................................................................... 118
Authentication and Authorization ...................................................................... 127
AJAX Enabling RSVPs Accepts ............................................................................. 138
Integrating an AJAX Map .................................................................................... 146
Unit Testing ....................................................................................................... 165
NerdDinner Wrap Up ......................................................................................... 186
Page 3 of 186
Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the
following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may
be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The
complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”
NerdDinner
The best way to learn a new framework is to build something with it. This first chapter walks through
how to build a small, but complete, application using ASP.NET MVC, and introduces some of the core
concepts behind it.
The application we are going to build is called “NerdDinner”. NerdDinner provides an easy way for
people to find and organize dinners online:
NerdDinner enables registered users to create, edit and delete dinners. It enforces a consistent set of
validation and business rules across the application: