Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you display a users full name in the login partial view in ASP.NET Core

Tags:

asp.net-core

I'm new to ASP.NET Core, most of my existing experience is with Java/PHP, and a little ASP.NET MVC about 10 years ago.

I'm using a Mac so I'm trying to build a project using Visual Studio Code and ASP.NET Core.

I've created a web application using 'yo'. I've changed the default database connection string to use an MSSQL database I have on a server. I've ran the dotnet ef database update command to create the necessary tables in the database.

I wanted to add firstname and lastname to the user, so I've created the columns in the AspNetUser table, and edited the ApplicationUser class to reflect this;

namespace pabulicms.Models
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser
    {    
        public string Firstname { get; set; }
        public string Lastname { get; set; }
    }
}

I've gone ahead and amended the view model for the registration form to include the firstname and lastname, and I've updated the Signup method so that the firstname and lastname is saved to the database.

By default the _LoginPartial.cshtml view displays the users username(email address), I'd like to change this to display the users full name, but I'm unsure as to how I do this.

This is what the _LoginPartial looks like at the moment;

@using Microsoft.AspNetCore.Identity
@using pabulicms.Models

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@if (SignInManager.IsSignedIn(User))
{
    <form asp-area="" asp-controller="Account" asp-action="Logout" method="post" id="logoutForm" class="navbar-right">
        <ul class="nav navbar-nav navbar-right">
            <li>
                <a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
            </li>
            <li>
                <button type="submit" class="btn btn-link navbar-btn navbar-link">Log out</button>
            </li>
        </ul>
    </form>
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li><a asp-area="" asp-controller="Account" asp-action="Register">Register</a></li>
        <li><a asp-area="" asp-controller="Account" asp-action="Login">Log in</a></li>
    </ul>
}

It's obviously this line I need to change;

Hello @UserManager.GetUserName(User)!

However changing it to @UserManager.GetFirstname(User)! doesn't work, as it tells me that the method GetFirstname doesn't exist;

like image 773
SheppardDigital Avatar asked May 10 '17 11:05

SheppardDigital


People also ask

What is partial view in asp net core?

A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.

How do I create a partial view in .NET core?

The method name contains Async will be rendered for the asynchronous code. The Render methods result need to be written directly to the response. If we want to Create Partial View, then we need to right click on the view folder > click on the folder > select Add > click on View.

How can you display the partial view on the main page?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.


1 Answers

I searched a lot and finally found this solution:

Change this: @UserManager.GetUserName(User)

To this: @UserManager.GetUserAsync(User).Result.LastName

refer to: Link

like image 198
Mohammad Taghipour Avatar answered Oct 12 '22 22:10

Mohammad Taghipour