Question: Manage Extended ApplicationUser

Question

Manage Extended ApplicationUser

Answers 1
Added at 2016-01-10 10:01
Tags
Question

Using ASP.NET 5 Web Application Template and have extended ApplicationUser by adding BusinessName

    public class ApplicationUser : IdentityUser
{
    public string BusinessName { get; set; }

The BusinessName property in dbContext

    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 has also been added to 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; }
    }
}

The ManageController Index action is called

        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);
    }

and also to 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>

What is the correct way to populate BusinessName?

Answers to

Manage Extended ApplicationUser

nr: #1 dodano: 2016-01-10 10:01

In your controller action you didn't set the BusinessName on your view model:

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