• 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; }
        }
  • 相关阅读:
    16进制字节和数字转换
    Vs2013 使用EF6 连接mysql数据库
    设计模式-单例模式(Singleton)
    WPF 10天修炼 第十天- WPF数据绑定
    WPF 10天修炼 第九天
    WPF 10天修炼 第八天
    WPF 10天修炼 第七天- WPF资源、样式、控件模板
    WPF 10天修炼 第六天- 系统属性和常用控件
    WPF 10天修炼 第五天- 内容控件
    WPF排版布局经验总结(干货)简短不疲倦
  • 原文地址:https://www.cnblogs.com/RobotTech/p/2120935.html
Copyright © 2020-2023  润新知