博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET MVC2 in Action 读书笔记 [3]
阅读量:6800 次
发布时间:2019-06-26

本文共 3922 字,大约阅读时间需要 13 分钟。

Chapter03:

AccountProfile

Index.aspx:

Profiles

 
Username
First name
Last name
Email
 
 
<% foreach (var profile in Model) { %>
 
 
<%= Html.Encode(profile.Username) %>
 
 
<%= Html.Encode(profile.FirstName) %>
 
 
<%= Html.Encode(profile.LastName) %>
 
 
<%= Html.Encode(profile.Email) %>
 
 
<%= Html.ActionLink("View Profile", "Show", new{username = profile.Username}) %>
 
 
<% } %>
 

Show.aspx:

View Profile
Fields

Username:
<%= Html.Encode(Model.Username) %>

FirstName:
<%= Html.Encode(Model.FirstName) %>

LastName:
<%= Html.Encode(Model.LastName) %>

Email:
<%= Html.Encode(Model.Email) %>

 

<%
bool hasPermission = (bool)ViewData["hasPermission"];
if (hasPermission)
{ %>
<%=Html.ActionLink("Edit", "Edit", new { username = Model.Username }) %>
|
<%=Html.ActionLink("Back to List", "Index") %>
<% } %>

Edit.aspx:

Edit

 
<% using (Html.BeginForm()) {
%>
 
<%= Html.EditorForModel() %>

 
<% } %>
 
<%=Html.ActionLink("Back to List", "Index") %>
 

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
_profiles = new List
();
 
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

你可能感兴趣的文章
DataTable 的数据导出到 Excel
查看>>
委托由浅入深学习
查看>>
BZOJ 1012 [JSOI2008]最大数maxnumber
查看>>
权限管理[Linux]
查看>>
unity3d优化总结篇(二)
查看>>
自定义view,实现文本自动换行
查看>>
查看网页自动保存的密码
查看>>
BZOJ2705:[SDOI2012]Longge的问题——题解
查看>>
AFNetworking
查看>>
python基础--内置函数map
查看>>
Protobuf3 序列化
查看>>
Chisel3 - model - UserModule commands
查看>>
下载新浪的行情数据
查看>>
六,移植uboot-设置默认环境变量,完善u-boot
查看>>
【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
查看>>
新博客
查看>>
jquery $.proxy使用
查看>>
Hello,C++(7)函数模板和类模板
查看>>
网站使用https协议
查看>>
git 使用
查看>>