확장 된 응용 프로그램 사용자 관리

asp.net-core asp.net-core-mvc asp.net-identity-3 entity-framework-core

문제

ASP.NET 5 웹 응용 프로그램 템플릿을 사용하고 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을 채우는 올바른 방법은 무엇입니까?

수락 된 답변

컨트롤러 작업에서 뷰 모델에 BusinessName 을 설정하지 않았습니다.

var user = await GetCurrentUserAsync();
var model = new IndexViewModel
{
    BusinessName = user.BusinessName,
    ...
};



아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow
이 KB는 합법적입니까? 예, 이유를 알아보십시오.
아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow
이 KB는 합법적입니까? 예, 이유를 알아보십시오.