I'm still learning MVC5 and I'm having issues figuring out how to output data from a user's table. I added fields to the register form that takes a phone number and driver license number and I can successfully send the data to the database, but I'm having issues retrieving it and displaying it in the view for logged in user. I've looked at examples, but I must be missing something because I can output some of the data, but not the fields I want specifically. (Phone number and driver license number)
Here's my code
Index.cshtml
User Data:
@{
System.Security.Claims.ClaimsPrincipal claimsPrincipal = Context.User
as System.Security.Claims.ClaimsPrincipal;
System.Security.Claims.ClaimsIdentity id = claimsPrincipal.Identity
as System.Security.Claims.ClaimsIdentity;
foreach (var claim in id.Claims)
{
<p>Claim: @claim.ToString()</p>
<p>Claim More: @claim.p</p>
}
}
HomeController
[Authorize]
public ActionResult Index()
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationUser.MyDbContext()));
// Get the current logged in User and look up the user in ASP.NET Identity
var currentUser = manager.FindById(User.Identity.GetUserId());
// Recover the profile information about the logged in user
ViewBag.DriverLicense= currentUser.DrivingLicense;
ViewBag.PhoneNumbers= currentUser.PhoneNumbers;
return View();
}
ApplicationUser.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace Vidly2.Models
{
public class ApplicationUser : IdentityUser
{
[Required]
[StringLength(255)]
public string DrivingLicense { get; set; }
[Required]
public int PhoneNumbers { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public class MyDbContext : IdentityDbContext<ApplicationUser>
{
public MyDbContext()
: base("DefaultConnection")
{
}
}
}
}
Let me know if you need any additional info