20 Recipes For Programming Mvc 3
Jamie Munro
Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo
20 Recipes for Programming MVC 3
by Jamie Munro
Copyright © 2011 Jamie Munro. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational,business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editors: Shawn Wallace and Mike Hendrickson Production Editor: Kristen Borg Proofreader: O’Reilly Production Services Revision History for the First Edition:Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano
2011-09-27 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449309862 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. 20 Recipes for Programming MVC 3, the image of a Garganey duck, and relatedtrade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of thisbook, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
ISBN: 978-1-449-30986-2 [LSI] 1317043462
To my wife and kids: you are a dream come true!
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . vii The Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.20 1.21 Restricting Access to Views with Password Protection Automating Generation of Controllers and Views Validating User InputImplementing Multiple Languages Sending a Welcome Email Retrieving a Forgotten Password Sorting a List of Results Paging Through a List of Results Filtering a List of Results Searching a List of Results by Keyword Uploading a File Through a Form Resizing an Image to Create a Thumbnail Implementing Ajax to Enhance the User Experience Submitting a Form with Ajax Enabling a CAPTCHA Mobilizing Your WebsitePaging Through Content Without the Pages Displaying Search Results While Typing Routing Users to a Specific Controller and Action Caching Results for Faster Page Loads Going Further 1 5 9 13 21 24 30 34 39 45 50 57 60 67 74 78 85 89 94 101 106
v
Preface
About The Book
The goal of a Model-View-Controller (MVC) framework is to allow developers to easily separate their code in distinctaspects to simplify development tasks. The model layer allows us to integrate with data; usually a database table. The view layer allows us to represent our data in a visual fashion using a combination of HTML and CSS. The controller layer is the middleman between the model and view. The controller is used to retrieve data from a model and make that data available for a view. The goal of this book is toprovide web developers a cookbook of “recipes” that are required by many developers on a day-to-day basis. Each code sample contains a complete working example of how to implement authentication, email, AJAX, data validation, and many other examples. You will quickly find yourself referring to one of these samples for every website that you build.
Prerequisites
Before beginning with this...
Regístrate para leer el documento completo.