How to add a view for current user details in MVC?
MVC 2, so I do not know if it is different. I am trying to add a pag开发者_如何学运维e so that when a user is logged in, they click on their username at the upper right of the page, and it takes them to a page showing their details (email, change password link, profile information, etc..). I am trying to use the aspnet MembershipService to do this.
In your controller action do something like :
string id = HttpContext.User.Identity.Name.ToString();
ProfileBase profileBase;
if (!String.IsNullOrEmpty(id))
profileBase = ProfileBase.Create(id);
else
profileBase = HttpContext.Profile as ProfileBase;
With the profileBase object you can get all the profile attributes:
profileBase.GetPropertyValue("PersonalInformation.FirstName")
With these properties you can fill a custom view model object, for example:
public class ProfileInformation
{
public string FirstName { get; set; }
}
and pass it to the view:
return View(profileInformation);
In the view declare you''ll receive a ProfileInformation object like this :
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<AzureBright.Models.ProfileInformation>" %>
And then generate the Editor fields like this:
<%= Html.EditorFor(profile => profile)%>
Hope this is what you wanted to know
It will be the same as in 1.0, and as asked here: How to get the current user in ASP.NET MVC
精彩评论