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");
        }
 
    }

转载于:https://www.cnblogs.com/RobotTech/archive/2011/07/29/2121023.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值