0
我已經添加了如下獲得一些更多的用戶信息,並更新了數據庫,它的一切工作,因爲它應該,但即時通訊嘗試添加到管理文件夾的索引視圖,以便FullName,出生日期和國家將顯示,但我不能真正得到它的工作...有人可以指出我在正確的方向嗎?從數據庫訪問信息asp.net mvc 5
的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,出生日期和全名!耶,我,現在我想給用戶選擇編輯他們的信息......這將是一個更難的餅乾崩潰,我猜!
Index.cshtml視圖在哪裏管理?你有更新,顯示新的屬性?你可以發佈它的內容嗎? – Jack
@Jack編輯後,你可以看到我沒有得到這個... –
好的,你有一個索引視圖。因此,將它的模型設置爲您的RegisterViewModel並在您的視圖中插入@ model.FullName等。現在你應該在你的控制器中有一個對應的Index動作,你可以填充該視圖模型並顯示該視圖。不知道你想用底部的代碼來完成什麼。 –