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; }
        }
  • 相关阅读:
    整站爬虫命令
    小故事集锦
    中国最经典广告语大全
    常用的正则表达式
    特殊成员方法
    使用super函数----增量重写普通方法和构造方法
    重写普通方法和构造方法------原类的方法会被覆盖
    Python的数据类型与数据结构
    类和对象
    生产者-消费者问题与quene模块
  • 原文地址:https://www.cnblogs.com/RobotTech/p/2123737.html
Copyright © 2011-2022 走看看