ASP. NET MVC2 in action Reading Notes [1]

Source: Internet
Author: User

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 ;}
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.