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; }
    }
}
posted @ 2011-07-29 13:50  RobotTech  阅读(442)  评论(0编辑  收藏  举报