Chapter01:
1). Guestbook
Index. aspx:
<Form Method= "Post" Action= "/Guestbook/sign">
<Fieldset>
<Legend>Guest book</Legend>
<%= Html. Label ("Name")%>
<%= Html. Textbox ("Name")%>
<%= Html. Label ("Email")%>
<%= Html. Textbox ("Email")%>
<%= Html. Label ("Comments")%>
<%= Html. textarea ("Comments",New{Rows = 6, cols = 30 })%>
<Div>
<Input Type= "Submit" Value= "Sign" />
</Div>
</Fieldset>
</Form>
Thankyou. aspx:
<H2>Thank you!</H2>
<P>Thank you for signing the guest book! You entered:</P>
Name:<%= Viewdata ["Name"]%><BR />
Email:<%= Viewdata ["Email"]%><BR />
Comments:<I><%= Viewdata ["Comments"]%></I>
Guestbookcontroller. CS:
Public ClassGuestbookcontroller: Controller
{
PublicActionresult index ()
{
ReturnView ();
}
PublicActionresult sign (StringName,StringEmail,StringComments)
{
// Do something with the values, such as send an email
Viewdata ["Name"] = Name;
Viewdata ["Email"] = Email;
Viewdata ["Comments"] = Comments;
ReturnView ("Thankyou");
}
}
2). guestbookwithmodel
Index. aspx:
<H2>Sign the guest book!</H2>
<% Using(Html. beginform ()){%>
<Fieldset>
<Legend>Fields</Legend>
<P>
<%= Html. labelfor (model => model. Name)%>
<%= Html. textboxfor (model => model. Name)%>
</P>
<P>
<%= Html. labelfor (model => model. Email)%>
<%= Html. textboxfor (model => model. Email)%>
</P>
<P>
<%= Html. labelfor (model => model. Comments)%>
<%= Html. textareafor (model => model. Comments)%>
</P>
<P>
<Input Type= "Submit" Value= "CREATE" />
</P>
</Fieldset>
<%}%>
Thankyou. aspx:
<H2>Thank you!</H2>
Thank you for signing our guest book. You entered:<BR />
<%= Html. displayformodel ()%>
Guestbookcontroller. CS:
Public ClassGuestbookcontroller: Controller
{
PublicActionresult index ()
{
VaR model =NewGuestbookentry ();
ReturnView (model );
}
[Httppost]
PublicActionresult index (guestbookentry)
{
// Hang on to the submitted value, so we can
// Retrieve it upon redirect
Tempdata ["Entry"] = Entry;
ReturnRedirecttoaction ("Thankyou");
}
PublicActionresult thankyou ()
{
If(Tempdata ["Entry"] =Null)
{
// Somehow they got here without filling out the form
ReturnRedirecttoaction ("Index");
}
VaR model = (guestbookentry) tempdata ["Entry"];
ReturnView (model );
}
}
Guestbookentry. CS:
Public ClassGuestbookentry
{
Public StringName {Get; set ;}
Public StringEmail {Get; set ;}
Public StringComments {Get; set ;}
}