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

    Chapter04:

    ControllerExamples

    Index.aspx:

    <h2>Index</h2>
        <%if (TempData.ContainsKey("message"))
          {%>
        <p class="input-validation-success"><%=TempData["message"]%></p>
        <%} %>
        <table>
            <tr>
                <th>
                    Username
                </th>
                <th>
                    Name
                </th>
            </tr>
     
        <% foreach (var item in Model) { %>
        
            <tr>
                <td>
                    <%= Html.Encode(item.Username) %>
                </td>
                <td>
                    <%= Html.Encode(item.Name) %>
                </td>
            </tr>
        
        <% } %>
     
        </table>

    Display.aspx:

    <h2>Display</h2>
     
        <fieldset>
            <legend>Fields</legend>
            <p>
                Username:
                <%= Html.Encode(Model.Username) %>
            </p>
            <p>
                Name:
                <%= Html.Encode(Model.Name) %>
            </p>
        </fieldset>

    Edit.aspx:

    <h2>Edit</h2>
     
     
        <% using (Html.BeginForm()) {%>
            <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
     
            <fieldset>
                <legend>Fields</legend>
                <p>
                    <label for="Username">Username:</label>
                    <%= Html.TextBox("Username", Model.Username) %>
                    <%= Html.ValidationMessage("Username", "*") %>
                </p>
                <p>
                    <label for="FirstName">FirstName:</label>
                    <%= Html.TextBox("FirstName", Model.FirstName) %>
                    <%= Html.ValidationMessage("FirstName", "*") %>
                </p>
                <p>
                    <label for="LastName">LastName:</label>
                    <%= Html.TextBox("LastName", Model.LastName) %>
                    <%= Html.ValidationMessage("LastName", "*") %>
                </p>
                <p>
                    <input type="submit" value="Save" />
                </p>
            </fieldset>
     
        <% } %>
     
        <div>
            <%=Html.ActionLink("Back to List", "Index") %>
        </div>

    UserController.cs:

    public class UserController : Controller
        {
            public ActionResult Index()
            {
                IEnumerable<User> users =
                    UserRepository.GetAll();
     
                UserDisplay[] viewModel = users.Select(
                    user => new UserDisplay
                                {
                                    Username = user.Username,
                                    Name =
                                        user.FirstName + " " +
                                        user.LastName
                                }).ToArray();
     
                return View(viewModel);
            }
     
            [HttpGet]
            public ActionResult Display(int Id)
            {
                User user = UserRepository.GetById(Id);
     
                var viewModel = new UserDisplay
                                    {
                                        Username = user.Username,
                                        Name =
                                            user.FirstName + " " +
                                            user.LastName
                                    };
                return View(viewModel);
            }
     
            [HttpGet]
            public ActionResult Edit(int Id)
            {
                User users = UserRepository.GetById(Id);
     
                var viewModel = new UserInput
                                    {
                                        Username =
                                            users.Username,
                                        FirstName =
                                            users.FirstName,
                                        LastName =
                                            users.LastName,
                                    };
                return View(viewModel);
            }
     
     
            [HttpPost]
            public ActionResult Edit(UserInput input)
            {
                if (ModelState.IsValid)
                {
                    UpdateUserFromInput(input);
                    TempData["message"] = "The user was updated";
                    return RedirectToAction("index");
                }
     
                return View(input);
            }
     
            private void UpdateUserFromInput(UserInput input)
            {
                User user =
                    UserRepository.GetByUsername(input.Username);
                user.FirstName = input.FirstName;
                user.LastName = input.LastName;
                UserRepository.Save(user);
            }
        }
     
        public class UserDisplay
        {
            public string Username { get; set; }
            public string Name { get; set; }
        }
     
        public class UserInput
        {
            [Required]
            public string Username { get; set; }
     
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
     
     
        public static class UserRepository
        {
            public static IEnumerable<User> GetAll()
            {
                return new[]
                           {
                               new User
                                   {
                                       Username = "FFlintstone",
                                       FirstName = "Fred",
                                       LastName = "Flintstone"
                                   },
                               new User
                                   {
                                       Username = "brubble",
                                       FirstName = "Barney",
                                       LastName = "Rubble"
                                   },
                               new User
                                   {
                                       Username = "bbrubble",
                                       FirstName = "Bamm-Bamm",
                                       LastName = "Rubble"
                                   },
                               new User
                                   {
                                       Username = "wilma",
                                       FirstName = "Wilma",
                                       LastName = "Flintstone"
                                   },
                               new User
                                   {
                                       Username = "pebbles",
                                       FirstName = "Pebbles",
                                       LastName = "Flintstone"
                                   },
                               new User
                                   {
                                       Username = "dino",
                                       FirstName = "Dino",
                                       LastName = "Flintstone"
                                   },
                           };
            }
     
            public static User GetById(int id)
            {
                if (id == 2)
                    return new User();
                return new User
                           {
                               Username = "FFlintstone",
                               FirstName = "Fred",
                               LastName = "Flintstone"
                           };
            }
     
            public static User GetByUsername(string username)
            {
                return
                    GetAll().Where(s => s.Username == username).
                        FirstOrDefault();
            }
     
            public static void Save(User user)
            {
                
            }
        }
     
        public class User
        {
            public string Username { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
  • 相关阅读:
    智能指针之 auto_ptr
    UML在线绘图
    inline使用
    工作随笔—2017-12-12
    链表排序
    转——浅谈如何提高服务器并发处理能力
    使用re开发python计算器
    Linux-centos7下python3 环境设置
    C语言中的static 详细分析
    pycharm import pygame 出现报错:No module named 'pygame'
  • 原文地址:https://www.cnblogs.com/RobotTech/p/2123737.html
Copyright © 2011-2022 走看看