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; }
        }
  • 相关阅读:
    CSS颜色代码大全
    swfuploadphp上传说明
    projectlocker 使用
    十大简单易用的免费在线HTML编辑器
    c#读取docx(ooxml)
    直接在网页上显示word2007文档
    树莓派4b noMachine远程连接4000端口问题
    visual studio问题集合
    设置TrackMouseEvent捕获WM_MOUSEHOVER和WM_MOUSELEAVE消息
    atlwin中不停发WM_PAINT消息原因分析
  • 原文地址:https://www.cnblogs.com/RobotTech/p/2120935.html
Copyright © 2011-2022 走看看