使用ASP.NET 5 Web应用程序模板,并通过添加BusinessName
扩展了ApplicationUser
public class ApplicationUser : IdentityUser
{
public string BusinessName { get; set; }
dbContext中的BusinessName属性
public partial class CustomersContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>(entity =>
{
entity.HasIndex(e => e.BusinessName).HasName("BusinessNameIndex");
entity.Property(e => e.BusinessName)
.HasMaxLength(255)
.HasColumnType("varchar");
BusinessName也已添加到ViewModels / Manage / IndexViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
namespace Customers.Web.ViewModels.Manage
{
public class IndexViewModel
{
public bool HasPassword { get; set; }
public IList<UserLoginInfo> Logins { get; set; }
public string BusinessName { get; set; }
public string PhoneNumber { get; set; }
public bool TwoFactor { get; set; }
public bool BrowserRemembered { get; set; }
}
}
ManageController Index操作被称为
public async Task<IActionResult> Index(ManageMessageId? message = null)
{
ViewData["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 user = await GetCurrentUserAsync();
var model = new IndexViewModel
{
HasPassword = await _userManager.HasPasswordAsync(user),
PhoneNumber = await _userManager.GetPhoneNumberAsync(user),
TwoFactor = await _userManager.GetTwoFactorEnabledAsync(user),
Logins = await _userManager.GetLoginsAsync(user),
BrowserRemembered = await _signInManager.IsTwoFactorClientRememberedAsync(user)
};
return View(model);
}
以及Views / Manage / Index.cshml
@model IndexViewModel
@{
ViewData["Title"] = "Manage your account";
}
<h2>@ViewData["Title"].</h2>
<p class="text-success">@ViewData["StatusMessage"]</p>
<div>
<h4>Change your account settings</h4>
<hr />
<dl class="dl-horizontal">
<dt>External Logins:</dt>
<dd>
@Model.Logins.Count [<a asp-controller="Manage" asp-action="ManageLogins">Manage</a>]
</dd>
<dt>Business Name:</dt>
<dd>
@Model.BusinessName
</dd>
填充BusinessName的正确方法是什么?
答案 0 :(得分:1)
在您的控制器操作中,您没有在视图模型上设置BusinessName
:
var user = await GetCurrentUserAsync();
var model = new IndexViewModel
{
BusinessName = user.BusinessName,
...
};