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

    Chapter03:

    AccountProfile

    Index.aspx:

    <h2>Profiles</h2>
        <table>
            <tr>
                <th>Username</th>
                <th>First name</th>
                <th>Last name</th>
                <th>Email</th>
                <th>&nbsp;</th>
            </tr>
            <% foreach (var profile in Model) { %>
            <tr>
                <td>
                    <%= Html.Encode(profile.Username) %>
                </td>
                <td>
                    <%= Html.Encode(profile.FirstName) %>
                </td>
                <td>
                    <%= Html.Encode(profile.LastName) %>
                </td>
                <td>
                    <%= Html.Encode(profile.Email) %>
                </td>
                <td>
                    <%= Html.ActionLink("View Profile", "Show", new{username = profile.Username}) %>
                </td>
            </tr>
            <% } %>
        </table>

    Show.aspx:

    <h2>
            View Profile</h2>
        <fieldset>
            <legend>Fields</legend>
            <p>
                Username:
                <%= Html.Encode(Model.Username) %>
            </p>
            <p>
                FirstName:
                <%= Html.Encode(Model.FirstName) %>
            </p>
            <p>
                LastName:
                <%= Html.Encode(Model.LastName) %>
            </p>
            <p>
                Email:
                <%= Html.Encode(Model.Email) %>
            </p>
        </fieldset>
        <p>
            <% 
                bool hasPermission = (bool)ViewData["hasPermission"];
                if (hasPermission)
                { %>
            <%=Html.ActionLink("Edit", "Edit", new { username = Model.Username }) %>
            |
            <%=Html.ActionLink("Back to List", "Index") %>
            <% } %>
        </p>

    Edit.aspx:

    <h2>Edit</h2>
     
        <% using (Html.BeginForm()) {%>
        
            <%= Html.EditorForModel() %>
            <p>
                <button type="submit" value="Save" name="SaveButton">Save</button>
                <button type="submit" value="SaveAndClose" name="SaveButton">Save and Close</button>
            </p>
     
        <% } %>
     
        <div>
            <%=Html.ActionLink("Back to List", "Index") %>
        </div>

    ProfileModels.cs:

    public class Profile
        {
            public Profile(string username)
            {
                Username = username;
            }
     
            public string Username { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Email { get; set; }
        }
     
        public class ProfileEditModel
        {
            public ProfileEditModel(Profile profile)
            {
                Username = profile.Username;
                FirstName = profile.FirstName;
                LastName = profile.LastName;
                Email = profile.Email;
            }
     
            public ProfileEditModel()
            {
            }
     
            public string Username { get; set; }
     
            [DisplayName("First Name")]
            public string FirstName { get; set; }
     
            [DisplayName("Last Name")]
            public string LastName { get; set; }
     
            public string Email { get; set; }
        }
     
        public interface IProfileRepository
        {
            Profile[] GetAll();
            Profile Find(string username);
            void Add(Profile profile);
        }
     
        public class ProfileRepository : IProfileRepository
        {
            private static List<Profile> _profiles = new List<Profile>();
     
            static ProfileRepository() {
                _profiles.Add(new Profile("JPalermo") { FirstName = "Jeffrey", LastName = "Palermo", Email = "jeffrey@MVC2Demo.example" });
                _profiles.Add(new Profile("BScheirman") { FirstName = "Ben", LastName = "Scheirman", Email = "ben@MVC2Demo.example" });
                _profiles.Add(new Profile("MHinze") { FirstName = "Matt", LastName = "Hinze", Email = "matt@MVC2Demo.example" });
                _profiles.Add(new Profile("JBogard") {  FirstName = "Jimmy", LastName = "Bogard", Email = "jimmy@MVC2Demo.example" });
                _profiles.Add(new Profile("EHexter") { FirstName = "Eric", LastName = "Hexter", Email = "eric@MVC2Demo.example" });
     
            }
     
            public Profile[] GetAll()
            {
                return _profiles.ToArray();
            }
     
            public Profile Find(string username)
            {
                var profile = _profiles.FirstOrDefault(p => p.Username == username);
                if (profile == null)
                {
                    profile = new Profile(username);
                    Add(profile);
                }
     
                return profile;
            }
     
            public void Add(Profile profile)
            {
                _profiles.Add(profile);
            }
        }

    ProfileController.cs:

    public class ProfileController : Controller
        {
            private readonly IProfileRepository _profileRepository;
     
            public ProfileController(IProfileRepository profileRepository)
            {
                _profileRepository = profileRepository;
            }
     
            public ProfileController() : this(new ProfileRepository()) { }
     
            public ViewResult Index()
            {
                var profiles = _profileRepository.GetAll();
                return View(profiles);
            }
     
            public ViewResult Show(string username)
            {
                var profile = _profileRepository.Find(username);
     
                bool hasPermission = User.Identity.Name == username;
     
                ViewData["hasPermission"] = true;
     
                return View(profile);
            }
     
            public ViewResult Edit(string username)
            {
                var profile = _profileRepository.Find(username);
                return View(new ProfileEditModel(profile));
            }
     
            public RedirectToRouteResult Save(ProfileEditModel form)   ??????????? 正确吗????
            {
                var profile = _profileRepository.Find(form.Username);
     
                profile.Email = form.Email;
                profile.FirstName = form.FirstName;
                profile.LastName = form.LastName;
     
                return RedirectToAction("Index");
            }
     
        }
  • 相关阅读:
    hadoop_05
    分布式集群时间同步
    crontab定时任务
    操作系统笔记五:输入输出系统
    操作系统笔记四:文件管理与磁盘存储器管理
    操作系统笔记三:存储器管理
    操作系统笔记二:进程与调度(2)
    操作系统笔记二:进程与调度(1)
    可能的加分项
    对老师的建议
  • 原文地址:https://www.cnblogs.com/RobotTech/p/2121023.html
Copyright © 2011-2022 走看看