zoukankan      html  css  js  c++  java
  • 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; }
        }
  • 相关阅读:
    [背包问题][二进制优化] Jzoj P4224 食物
    [并查集][排序] Jzoj P4223 旅游
    [哈夫曼树][优先队列] Bzoj P4198 荷马史诗
    [hash][差分][虚树] Jzoj P6011 天天爱跑步
    [dp] Jzoj P6012 荷马史诗
    [dp][递归] Jzoj P4211 送你一棵圣诞树
    [数学] Jzoj P3912 超氧化钾
    堆学习笔记(未完待续)(洛谷p1090合并果子)
    [AC自动机]luogu P2444 病毒
    [概率期望][DP]luogu P3830 随机树
  • 原文地址:https://www.cnblogs.com/RobotTech/p/2120935.html
Copyright © 2011-2022 走看看