§ 2. 4 A starter Application
In the remainder of this chapter, you'll learn some more of the basic ASP. net mvc principles by building a simple data entry application.
In the next chapter, you'll find a discussion ofKey MVC invalid tural principles, And the rest of the book will giveIncreasingly detailed explanationsAnd demonstrations of exactly all ASP. net mvc 2 features.
§ 2. 4.1 The story
Now, let us create a web site (about a party) that allows invitees to send back an electronic RSVP. This application namedPartyinvites, Will
- Have a home page showing information about the party
- Have an RSVP form into which invitees can enter their contact details and say whether or not they will attend
- Validate form submissions, displaying a thank you page if successful
- E-Mail details of completed RSVPs to the party organizer
OK. Let us start do the homepage, updateIndex. aspxView
<Body>
§ 2. 4.2 designing a data model
In MVC, M stands for model, and it's the most important character.
Your model is a software representation of the real-world objects, processes, and rules that make up the subject matter, or domain, of your application.
So we need to creat a type namedGuestresponse.This object will be responsibleStoring, validating, and ultimately confirmingAn invitee's RSVP.
§ 2. 4.3 adding a model class
Add a new, blank C # class calledGuestresponseInside/ModelsFolder
Namespace mvcprogram. Models {public class guestresponse {public string name {Get; set;} Public String email {Get; set;} Public String phone {Get; set;} public bool? Willattend {Get; Set ;}}}
HereWillattendIs a nullable bool (? Mark makes it nullable). This creates a tri-state value:True, false, or null, It used Specified whether the guest'll attend.
§ 2. 4.4 linking between actions
There's going to be an RSVP form, so you'll need to place a link to it. UpdateIndex. aspx ViewAgain as follows:
<Body>
Note:Html. actionlink isA html helper Method. The framework comes with a built-in collection of useful HTML helpers that give you a convenient shorthand for rendering not just html links, but also text input boxes, Check boxes, selection boxes, and so on, and even custom controls. When you type <%: HTML. Or <% = html ., You'll see Visual Studio's intelliisense spring forward to let you pick from the available HTML helper methods. They're each explained in Chapter 11, though most are obvious. |
Run the project again, and you'll see the new link, as shown:
Figure2-9. A view with a link
But if you click the RSVP now link, you'll get a 404 Not Found error.
So we need creat a new file like the error says. It will readHttp: // yourserver/home/rsvpform
Add a new method to yourHomecontrollerClass
Public actionresult rsvpform () {return view ();}
§ 2. 4.5 introducing stronugly typed views
Again, you'll need to add a view for that new action. But first, make sure you 've compiled your code. And then right clickViews folderCreat a new view, features like this:
When you click Add, you'll get a new view at this action's default view location,~ /Views/home/rsvpform. aspx.
§ 2. 4.6 building a form
TurningRsvpform. aspxInto a form for editing instancesGuestresponse
<Body>
These HTML helpers let you pick out a model property using a Lambda expression (I. e., the X => X. propertyname syntax). This is only possible because your view isStronugly typed.
You can pass parametersHtml. beginform (),Telling it which action method the form shoshould post to when submitted.
§ 2. 4.7 handling form submissions
In order to receive and process submitted form data. We'llSlice the rsvpform action down the middle, Creating the following:
- A method that responds to http get requests: Note that a GET request is what a browser issues normally each time someone clicks a link. This version of the action will be responsible for displaying the initial blank form when someone first visits/Home/rsvpform.
- A method that responds to http post requests: By default, forms rendered using HTML. beginform () are submitted by the browser asPostRequest. This version of the action will be responsible for processing ing submitted data and deciding what to do with it.
The two methods have totally different responsibilities. However, from outside, the pair of C # methods will be seenA single logical action, Since they will haveThe same name and are invoked by requesting the same URL
§ 2. 4.6 introducing model binding
The second overload takes an instanceGuestresponseAs a parameter. Given that the method is being invoked via an HTTP request, and thatGuestresponseIs A. Net type that is totally unknown to HTTP, how can an HTTP request possibly supply a guestresponse instance? The answer isModel binding, An extremely useful feature of ASP. net MVC whereby incoming data is automatically parsed and used to populate action method parameters by matching incoming key/value pairs with the names of properties on the desired. net type.
Let you work primarily in terms of stronugly typed. Net objects rather than low-level fiddling with request. Form [] and request. querystring [] dictionaries
§ 2. 4.7 rendering arbitrary views and passing a model object to them
Return view ("thanks", guestresponse );
This line tells ASP. net mvc to find and render a view calledThanks, And to supplyGuestresponseObject To that view. Since this all happens in a controller calledHomecontroller, ASP. net mvc will keep CT to find the thanks view~ /Views/home/Thanks. aspx, But of course no such file yet exists. Let's create it.
After you creat it and fill the Code as follows:
<Body>
Then you can fire up your application. And see the welcome page
§ 2. 4.8 adding Validation
The. NET class library has a namespace calledSystem. componentmodel. dataannotationsThat includes attributes you can use to define validation rules declaratively.
Public class guestresponse {[required (errormessage = "Please enter your name")] public string name {Get; set;} [required (errormessage = "Please enter your email address")] [regularexpression (". + \\@. + \\.. + ", errormessage =" Please enter a valid email address. ")] Public String email {Get; set;} [required (errormessage =" Please enter your phone number ")] Public String phone {Get; set;} [required (erro Rmessage = "Please make sure if you want to come")] public bool? Willattend {Get; Set ;}}
After that, let's update the secondRsvpform ()Action method so that if there were any validation errors:
[Httppost] public actionresult rsvpform (guestresponse) {If (modelstate. isvalid) {// to do: e-mail guestresponse to the party organizer return view ("thanks", guestresponse);} else {return view ();}}
Finally, choose where to display any validation error messages by addingHtml. validationsummary ()To the form inRsvpform. aspx View
<H1> RSVP
§ 2. 4.9Model binding tells input controls to redisplay user-entered values
I mentioned previusly that becauseHTTP is Stateless, You shouldn't expect CT input controls to retain state then ss multiple requests. however, because you're now using model binding to parse the incoming data, you'll find that when you redisplay the form after a validation error, the input controls will redisplay any user-entered values.
§ 2. 4.10 highlighting invalid Fields
The easiest way to highlight invalid fields is to reference a CSS style sheet,/Content/site.css,
Go to yourRsvpform. aspxView and add a new stylesheet reference to its<Head>Section:
<Head runat = "server"> <title> rsvpform </title> <LINK rel = stylesheet href = "http://www.cnblogs.com/Content/Site.css"/>
Then run the application. As shown.
§ 2. 4.11 finishing off
To construct the outgoing e-mail, start by adding the following methodGuestresponse:
Private mailmessage buildmailmessage () {var message = new stringbuilder (); message. appendformat ("Date: {0: yyyy-mm-dd hh: mm} \ n", datetime. now); message. appendformat ("RSVP from: {0} \ n", name); message. appendformat ("Email: {0} \ n", email); message. appendformat ("Phone: {0} \ n", phone); message. appendformat ("can come: {0} \ n", willattend. value? "Yes": "no"); return New mailmessage ("rsvps@example.com", // from "party-organizer@example.com", // to name + (willattend. value? "Will attend": "won't attend"), // subject message. tostring () // body );}
Then creat a method to call the former one;
Public void submit () {New smtpclient (). Send (buildmailmessage ());}
Finally, callGuestresponse. Submit ()From the secondRsvpform ()Overload, thereby sendingGuest responseBy e-mail only if it's valid:
[Httppost] public actionresult rsvpform (guestresponse) {If (modelstate. isvalid) {// to do: e-mail guestresponse to the party organizer guestresponse. submit (); Return view ("thanks", guestresponse) ;}else {return view ();}}
The last thing you need to do is updateWeb. config
<System.net> <mailsettings> <SMTP deliverymethod = "specifiedpickupdirectory"> <network host = "ignored"/> <specifiedpickupdirectory pickupdirectorylocation = "E: \ Temp "/> </SMTP> </mailsettings> </system.net>
After you run the application, you will see the email inE: \ Temp