Chapter03:
AccountProfile
Index.aspx:
Profiles
Username
First name
Last name
<% 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
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");
}
}