ASP.NET MVC2 in Action 读书笔记 [1]

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 class GuestBookController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
 
        public ActionResult Sign(string name, string email, string comments)
        {
            //do something with the values, such as send an email
 
            ViewData["name"] = name;
            ViewData["email"] = email;
            ViewData["comments"] = comments;
 
            return View("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 class GuestBookController : Controller
    {
        public ActionResult Index()
        {
            var model = new GuestBookEntry();
            return View(model);
        }
 
        [HttpPost]
        public ActionResult Index(GuestBookEntry entry)
        {
            //hang on to the submitted value, so we can
            //retrieve it upon redirect
            TempData["entry"] = entry;
            return RedirectToAction("ThankYou");
        }
 
        public ActionResult ThankYou()
        {
            if(TempData["entry"] == null)
            {
                //somehow they got here without filling out the form
                return RedirectToAction("index");
            }
 
            var model = (GuestBookEntry) TempData["entry"];
            return View(model);
        }
    }

GuestBookEntry.cs:

public class GuestBookEntry
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Comments { get; set; }
    }
posted @ 2011-07-29 13:32  RobotTech  阅读(357)  评论(0编辑  收藏  举报