我还在学习MVC5,我在解决如何从用户表中输出数据方面遇到了问题。我在寄存器表单中添加了字段,其中包含电话号码和驱动程序许可证号码,我可以将数据成功发送到数据库,但是我在检索它并将其显示在登录用户的视图中时遇到问题。我看过一些例子,但我必须遗漏一些东西,因为我可以输出一些数据,但不能输出我想要的字段。 (电话号码和驾驶执照号码)
这是我的代码
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")
{
}
}
}
}
如果您需要任何其他信息,请与我们联系