从数据库asp.net mvc 5访问信息

时间:2016-01-07 20:19:16

标签: asp.net asp.net-mvc asp.net-mvc-5 user-accounts user-management

我已添加以下内容以获取更多用户信息,并更新数据库,这一切都可以正常工作,但是我试图在manage文件夹的索引视图中添加以便FullName,birthdate和Country将显示,但我真的无法工作......有人能指出我正确的方向吗?

的AccountController:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, FullName = model.FullName, Country = model.Country, BirthDate = model.BirthDate };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
            return RedirectToAction("Index", "Home");
        }
        AddErrors(result);
    }
    return View(model);
}

accountviewmodel:

public class RegisterViewModel
{
    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Full Name")]
    public string FullName { get; set; }

    [Required]
    public DateTime BirthDate { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Country")]
    public string Country { get; set; }
}

IdentityModels:

public class ApplicationUser : IdentityUser
{
    public string FullName { get; set; }
    public string Country { get; set; }
    public DateTime BirthDate { get; set; }
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
}

Index.cshtml

<div>
<h4>Change your account settings</h4>
<hr />
<dl class="dl-horizontal">
    <dt>Full Name:</dt>
    <dd></dd>

    <dt>Birth Date:</dt>
    <dd></dd>

    <dt>Country:</dt>
    <dd>
        Comming soon
    </dd>


    <dt>Password:</dt>
    <dd>
        [
        @if (Model.HasPassword)
        {
            @Html.ActionLink("Change your password", "ChangePassword")
        }
        else
        {
            @Html.ActionLink("Create", "SetPassword")
        }
        ]
        </dd>

AccountViewModels:

public class SetNameViewModel
{
    [Display(Name ="Set Name")]
    public string SetName { get; set; }
}

public class SetCountryViewModel
{
    [Display(Name = "Set Country")]
    public string SetCountry { get; set; }
}

ManageController:

   public async Task<ActionResult> Index(ManageMessageId? message)
    {
        ViewBag.StatusMessage =
            message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
            : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
            : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set."
            : message == ManageMessageId.Error ? "An error has occurred."
            : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added."
            : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed."
            : "";

        var userId = User.Identity.GetUserId();
         //ADDED THEESE LINES
     var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
           var currentUser = manager.FindById(User.Identity.GetUserId());
      ////
        var model = new IndexViewModel
        {
            HasPassword = HasPassword(),
            PhoneNumber = await UserManager.GetPhoneNumberAsync(userId),
            TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId),
            Logins = await UserManager.GetLoginsAsync(userId),
            BrowserRemembered = await        AuthenticationManager.TwoFactorBrowserRememberedAsync(userId),

        };
        return View(model);
    }

现在,如果我使用:

    @User.Identity.GetUserId()

在我的视图中,我获得了用户ID,但我如何获取全名等?

  @User.Identity.GetUserName()

返回id的用户名

加入

         ViewBag.CurrentUser = currentUser;

到我的managecontroller,现在我可以通过@Viewbag.CurrentUser.Country,BirthDate和Fullname获取我想要的东西!是的,现在我想让用户选择编辑他们的信息...这将是一个更难崩溃的饼干我猜!

1 个答案:

答案 0 :(得分:0)

添加ViewBag.CurrentUser = currentUser;管理控制器为我工作了!