Jason Morris
Jason Morris

Reputation: 33

How can I get ASP.NET Core identity phone number display on the profile page?

I implemented ASP.NET Core identity into an app, and implemented roles and it's all working very well. I added the phone number as a required field on user registration, it's writing to the SQL database as expected.

However, it is not showing the phone number on the profile page - I'd like for it to show the number so the user is aware their number is stored, or if they need to update the contact information. Any suggestions on how to display the phone number here?

The index.cshtml is as follows - oddly enough, it's returning the username without any logic here:

@page
@model IndexModel
@{
    ViewData["Title"] = "Profile";
    ViewData["ActivePage"] = ManageNavPages.Index;
}

<h3>@ViewData["Title"]</h3>
<partial name="_StatusMessage" for="StatusMessage" />
<div class="row">
    <div class="col-md-6">
        <form id="profile-form" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger" role="alert"></div>
            <div class="form-floating mb-3">
                <input asp-for="Username" class="form-control" placeholder="Please choose your                   username." disabled />
                <label asp-for="Username" class="form-label"></label>
            </div>
            <div class="form-floating mb-3">
                <input asp-for="Input.PhoneNumber" class="form-control" placeholder="Please enter    your phone number."/>
                <label asp-for="Input.PhoneNumber" class="form-label"></label>
                <span asp-validation-for="Input.PhoneNumber" class="text-danger"></span>
            </div>
            <button id="update-profile-button" type="submit" class="w-100 btn btn-lg btn-primary">Save</button>
        </form>
    </div>
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

Upvotes: 0

Views: 186

Answers (1)

user23190874
user23190874

Reputation:

In JavaScript, an object is not updated when its attribute changes. It means that Input keeps the old PhoneNumber in memory. A quick fix is to create a variable for PhoneNumber independent from Input.

@{
    ViewData["Title"] = "Profile";
    ViewData["ActivePage"] = ManageNavPages.Index;
    ViewData["PhoneNumber"] = Input.PhoneNumber;
}

and

<input asp-for="@ViewData['PhoneNumber']" class="form-control" placeholder="Please enter    your phone number."/>
<label asp-for="@ViewData['PhoneNumber']" class="form-label"></label>
<span asp-validation-for="@ViewData['PhoneNumber']" class="text-danger"></span>

You might need to find a way to get the data back to your controller.

Upvotes: 0

Related Questions