简体   繁体   中英

How do I display custom profile information with VS2013 MVC5 templates?

There are several tutorials for the new MVC5 templates that show you how to easily add new fields to a user's profile information. Here are a couple I have viewed:

http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

My second link is the only tutorial that even mentions displaying the information back, but the tutorial is difficult to follow and that portion has so little information.

I want to display the user's birthdate on the Manage Account page, and maybe allow it to be edited. I have tried the following in AccountControllers.cs:[HttpPost]Manage():

if (ModelState.IsValid)
{
    var currentUserID = User.Identity.GetUserId();
    var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
    var currentUser = manager.FindById(User.Identity.GetUserId());
    ViewData.Add("userName", currentUser.UserName);
    ViewData.Add("BirthDate", currentUser.BirthDate);
}

Using this code, the ViewBag items I set are both blank when I display them on Manage.cshtml with

<p>Birthdate is @ViewBag.BirthDate</p>
<p>ReturnURL is @ViewBag.ReturnUrl</p>
<p>UserName is @ViewBag.userName</p>

How do I show the information stored in the database?

I fixed this issue easily when I realized that the application will never hit the [HttpPost] version of the manage method until it's been submitted. I moved it into the default manage method and the code works just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM