title: Display Image from database in ASP.NET Core 3.1
body: <p>I want to display a profile picture from the uploaded Image but will have a default image if the user has not uploaded an image. I have this code which saves Image data to my database:</p>
  <pre><code>public class UserProfileModel : PageModel
      {
          private readonly UserManager&lt;ApplicationUser&gt; _userManager;
          public readonly IWebHostEnvironment _env;
          private readonly IPhoto _photo;
  
          public UserProfileModel(UserManager&lt;ApplicationUser&gt; userManager,
                                  IWebHostEnvironment env)
          {
              _userManager = userManager;
              _env = env;
          }
          [BindProperty]
          [Required]
          [MaxLength(30, ErrorMessage = &quot;USERNAME CANNOT EXCEED 30 LETTERS.&quot;)]
          public string Name { get; set; }
          [BindProperty]
          [Required]
          [DataType(DataType.Date)]
          public string DateOfBirth { get; set; }
          [BindProperty]
          [Required]
          [EmailAddress]
          [Display(Name = &quot;Email Address&quot;)]
          public string Email { get; set; }
          [BindProperty]
          [Required]
          [DataType(DataType.PhoneNumber)]
          public string Mobile { get; set; }
          [BindProperty]
          [Required]
          public int Gender { get; set; }
          [BindProperty]
          [Required]
          public string Country { get; set; }
          [BindProperty]
          public IFormFile Image { get; set; }
          public List&lt;SelectListItem&gt; Countrydropdownlist { get; set; }
          public void OnGet()
          {
              Countrydropdownlist = GetCountryItems();
          }
          public List&lt;SelectListItem&gt; GetCountryItems()
          {
              string filepath = Path.Combine(_env.ContentRootPath, &quot;CountryList.json&quot;);
              string jsonlist = System.IO.File.ReadAllText(filepath);
              var result = JsonConvert.DeserializeObject&lt;RootCountry&gt;(jsonlist);
              List&lt;SelectListItem&gt; _countrydropdownlist = new List&lt;SelectListItem&gt;();
              foreach (var nation in result.Countries)
              {
                  _countrydropdownlist.Add(new SelectListItem { Value = nation.Code.ToString(), Text = nation.Name });
              }
              return _countrydropdownlist;
          }
          public async Task&lt;IActionResult&gt; OnPostUpdate()
          {
              if (ModelState.IsValid)
              {
                  var user = await _userManager.FindByNameAsync(User.Identity.Name);
                  user.Name = Name;
                  user.DateOfBirth = DateOfBirth;
                  user.PhoneNumber = Mobile;
                  user.Email = Email;
                  user.Country = Country;
                  user.Gender = Gender;
                  if(Image != null)
                  {
                      if(Image.Length &gt; 0)
                      {
                          using var streamReader = Image.OpenReadStream();
                          using var memoryStream = new MemoryStream();
                          streamReader.CopyTo(memoryStream);
                          byte[] uploadedImage = memoryStream.ToArray();
                          user.ImageData = uploadedImage;
                      }
                  }
                  var result = await _userManager.UpdateAsync(user);
                  if (result.Succeeded)
                  {
                      //Repopulate dropdownlist
                      Countrydropdownlist = GetCountryItems();
                      return Page();
                  }
              }
              return Page();
          }
      }
  </code></pre>
  <p>So I'm looking for something to add to the result.succeeded where an uploaded image replaces the default photo and is displayed as an image source in my Razor page.
  My cshtml:</p>
  <pre><code>@page
  @model TravelMate.Pages.UserProfileModel
  &lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot; /&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, shrink-to-fit=no&quot; /&gt;
      @{
          ViewBag.Title = &quot;User Profile&quot;;
      }
      &lt;style&gt;
          img {
              border-radius: 50%;
          }
      &lt;/style&gt;
      &lt;link href=&quot;~/bootstrap/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class=&quot;container&quot;&gt;
          &lt;div class=&quot;row&quot;&gt;
              &lt;div class=&quot;col text-center&quot;&gt;
                  &lt;br /&gt;
                     &lt;img src=&quot;~/Image/GeneralUser.jpg&quot; alt=&quot;User Image&quot; style=&quot;height: 300px&quot;&gt;
                  &lt;br /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;form enctype=&quot;multipart/form-data&quot; method=&quot;post&quot; asp-page-handler=&quot;Update&quot;&gt;
              &lt;div asp-validation-summary=&quot;All&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
              &lt;div class=&quot;row&quot;&gt;
                  &lt;div class=&quot;col-md-6&quot;&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label asp-for=&quot;Name&quot;&gt;&lt;/label&gt;
                          &lt;input asp-for=&quot;Name&quot; class=&quot;form-control&quot; /&gt;
                          &lt;span asp-validation-for=&quot;Name&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;col-md-6&quot;&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label asp-for=&quot;DateOfBirth&quot;&gt;&lt;/label&gt;
                          &lt;input asp-for=&quot;DateOfBirth&quot; class=&quot;form-control&quot; /&gt;
                          &lt;span asp-validation-for=&quot;DateOfBirth&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class=&quot;row&quot;&gt;
                  &lt;div class=&quot;col-md-6&quot;&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label asp-for=&quot;Mobile&quot;&gt;&lt;/label&gt;
                          &lt;input asp-for=&quot;Mobile&quot; class=&quot;form-control&quot; /&gt;
                          &lt;span asp-validation-for=&quot;Mobile&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;col-md-6&quot;&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label asp-for=&quot;Email&quot;&gt;&lt;/label&gt;
                          &lt;input asp-for=&quot;Email&quot; class=&quot;form-control&quot; /&gt;
                          &lt;span asp-validation-for=&quot;Email&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class=&quot;row&quot;&gt;
                  &lt;div class=&quot;col-md-4&quot;&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label asp-for=&quot;Country&quot;&gt;&lt;/label&gt;
                          &lt;select asp-for=&quot;Country&quot; asp-items=&quot;@Model.Countrydropdownlist&quot;&gt;
                              &lt;option selected disabled&gt; ---Select Country---&lt;/option&gt;
                          &lt;/select&gt;
                          &lt;span asp-validation-for=&quot;Country&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;col-md-4&quot;&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label asp-for=&quot;Gender&quot;&gt;&lt;/label&gt;
                          &lt;select asp-for=&quot;Gender&quot; class=&quot;form-control&quot;&gt;
                              &lt;option value=&quot;0&quot; selected disabled&gt;Select Gender&lt;/option&gt;
                              &lt;option value=&quot;1&quot;&gt;Male&lt;/option&gt;
                              &lt;option value=&quot;2&quot;&gt;Female&lt;/option&gt;
                          &lt;/select&gt;
                          &lt;span asp-validation-for=&quot;Gender&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;col-md-4&quot;&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label asp-for=&quot;Image&quot;&gt;&lt;/label&gt;
                          &lt;div class=&quot;custom-file&quot;&gt;
                              &lt;input asp-for=&quot;Image&quot; class=&quot;form-control custom-file-input&quot; /&gt;
                              &lt;label class=&quot;custom-file-label&quot;&gt;Choose File...&lt;/label&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              @section Scripts{
                  &lt;script&gt;
                      $(document).ready(function () {
                          $('.custom-file-input').on(&quot;change&quot;, function () {
                              var fileName = $(this).val().split(&quot;\\&quot;).pop();
                              $(this).next('.custom-file-label').html(fileName);
                          });
                      });
                  &lt;/script&gt;
              }
              &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Update&lt;/button&gt;
          &lt;/form&gt;
      &lt;/div&gt;
      &lt;script src=&quot;~/jquery/jquery.slim.min.js&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;~/bootstrap/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Thank you.</p>