zoukankan      html  css  js  c++  java
  • ASP.NET MVC2 in Action 读书笔记 [2]

    Chapter02:

    1). InputModel

    New.aspx:

    <h2>New Customer</h2>
        <form action="<%= Url.Action("Save") %>" method="post">
            <fieldset>
                <div>
                    <%= Html.LabelFor(x => x.FirstName) %> 
                    <%= Html.TextBoxFor(x => x.FirstName) %>
                </div>
                <div>
                    <%= Html.LabelFor(x => x.LastName) %> 
                    <%= Html.TextBoxFor(x => x.LastName) %>
                </div>
                <div>
                    <%= Html.LabelFor(x => x.Active) %> 
                    <%= Html.CheckBoxFor(x => x.Active) %></div>
                <div>
                <button name="save">Save</button></div>    
            </fieldset>
        </form>

    Save.aspx:

    <h2>Customer</h2>
        <div>
            Customer was saved.
        </div>
        <div>First Name: <%= Model.FirstName %></div>
        <div>Last Name: <%= Model.LastName %></div>
        <div>Active: <%= Model.Active %></div>

    CustomerController.cs:

    public class CustomerController : Controller
        {
            public ViewResult New()
            {
                return View();
            }
     
            public ViewResult Save(NewCustomerInput input)
            {
                return View(input);
            }
        }

    NewCustomerInput.cs:

    public class NewCustomerInput
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public bool Active { get; set; }
        }

    2). DisplayModel

    Index.aspx:

    <h2>Customer Summary</h2>
        <table>
             <tr>
                  <th>Name</th>
                  <th>Active?</th>
                  <th>Service Level</th>
                  <th>Order Count</th>
             <th>Most Recent Order Date</th>
             </tr>
             <% foreach (var summary in Model) { %>
                    <tr>
                        <td><%= summary.Name %></td>
                        <td><%= summary.Active ? "Yes" : "No" %></td>
                        <td><%= summary.ServiceLevel %></td>
                        <td><%= summary.OrderCount %></td>
                        <td><%= summary.MostRecentOrderDate %></td>
                    </tr>
             <% } %>
        </table>

    CustomerSummaryController.cs:

    public class CustomerSummaryController : Controller
        {
            private readonly CustomerSummaries _customerSummaries = new CustomerSummaries();
     
            public ViewResult Index()
            {
                IEnumerable<CustomerSummary> summaries =
                    _customerSummaries.GetAll();
     
                return View(summaries);
            }
        }

    CustomerSummaries.cs:

    public class CustomerSummaries
        {
            public IEnumerable<CustomerSummary> GetAll()
            {
                return new[]
                           {
                               new CustomerSummary
                                   {
                                       Active = true,
                                    Name = "John Smith",
                                       MostRecentOrderDate = "02/07/10",
                                       OrderCount = "42",
                                       ServiceLevel = "Standard"
                                   },
                               new CustomerSummary
                                   {
                                       Active = false,
                                       Name = "Susan Power",
                                       MostRecentOrderDate = "02/02/10",
                                       OrderCount = "1",
                                       ServiceLevel = "Standard"
                                   },
                               new CustomerSummary
                                   {
                                       Active = true,
                                       Name = "Jim Doe",
                                       MostRecentOrderDate = "02/09/10",
                                       OrderCount = "7",
                                       ServiceLevel = "Premier"
                                   },
                           };
            }
        }

    CustomerSummary.cs:

    public class CustomerSummary
        {
            public string Name { get; set; }
            public bool Active { get; set; }
            public string ServiceLevel { get; set; }
            public string OrderCount { get; set;}
            public string MostRecentOrderDate { get; set; }
        }

    3). ComboModel

    Index.aspx:

    <h2>Customer Summary</h2>
        <form action="<%= Url.Action("Save") %>" method="post">
         <table>
              <tr>
                  <th>Name</th>
                  <th>Service Level</th>
                  <th>Order Count</th>
                  <th>Most Recent Order Date</th>
                  <th>Active?</th>
              </tr>
              
              <% int index = 0; foreach (var summary in Model) { %>
              
                  <tr>
                        <td><%= summary.FirstName %> <%= summary.LastName %></td>
                        <td><%= summary.ServiceLevel %></td>
                        <td><%= summary.OrderCount %></td>
                        <td><%= summary.MostRecentOrderDate %></td>
                        <td>
                            <%= Html.CheckBox("input[" + index + "].Active", summary.Input.Active) %>
                            <input type="hidden" value="<%= summary.Input.Number %>" name="<%= "input[" + index + "].Number" %>" value="<%= summary.Input.Number %>" />
                        </td>
                  </tr>
              
              <% index++; } %>
              
         </table>
         <button name="submit">Change Status</button>
         </form>

    Save.aspx:

    <h2>Saved Customers</h2>
        
        <% foreach (var input in Model) { %>
            <div>
                Customer number <%= input.Number %> is <%= input.Active? "Active" : "Inactive" %>
            </div>
        <% } %>

    CustomerSummaryController.cs:

    public class CustomerSummaryController : Controller
        {
            private readonly CustomerSummaries _customerSummaries = new CustomerSummaries();
     
            public ViewResult Index()
            {
                IEnumerable<CustomerSummary> summaries =
                    _customerSummaries.GetAll();
     
                return View(summaries);
            }
     
            public ViewResult Save
                (List<CustomerSummary.CustomerSummaryInput> input)
            {
                return View(input);
            }
        }
     
        public class CustomerSummaries
        {
            public IEnumerable<CustomerSummary> GetAll()
            {
                return new[]
                           {
                               new CustomerSummary
                                   {
                                       Input = new CustomerSummary.CustomerSummaryInput {Active = true, Number = 4},
                                       FirstName = "John",
                                       LastName = "Smith",
                                       MostRecentOrderDate = "02/07/10",
                                       OrderCount = "42",
                                       ServiceLevel = "Standard"
                                   },
                               new CustomerSummary
                                   {
                                       Input = new CustomerSummary.CustomerSummaryInput {Active = false, Number = 5},
                                       FirstName = "Susan",
                                       LastName = "Power",
                                       MostRecentOrderDate = "02/02/10",
                                       OrderCount = "1",
                                       ServiceLevel = "Standard"
                                   },
                               new CustomerSummary
                                   {
                                       Input = new CustomerSummary.CustomerSummaryInput {Active = true, Number = 6},
                                       FirstName = "Jim",
                                       LastName = "Doe",
                                       MostRecentOrderDate = "02/09/10",
                                       OrderCount = "7",
                                       ServiceLevel = "Premier"
                                   },
                           };
            }
        }

    CustomerSummary.cs:

    public class CustomerSummary
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string ServiceLevel { get; set; }
        public string OrderCount { get; set; }
        public string MostRecentOrderDate { get; set; }
     
        public CustomerSummaryInput Input { get; set; }
     
        public class CustomerSummaryInput
        {
            public int Number { get; set; }
            public bool Active { get; set; }
        }
    }
  • 相关阅读:
    20175314 《Java程序设计》第六周学习总结
    20175314 结队编程项目——四则运算第一周
    20175314 《Java程序设计》迭代和JDB
    20175314 实验一 Java开发环境的熟悉
    20175314 《Java程序设计》第五周学习总结
    20175314 《Java程序设计》第四周学习总结
    20175314 《Java程序设计》第三周学习总结
    20175314 《Java程序设计》第二周学习总结
    20175314 《Java程序设计》第一周学习总结
    多态的成员特点
  • 原文地址:https://www.cnblogs.com/RobotTech/p/2120964.html
Copyright © 2011-2022 走看看