Search code examples
asp.net-coreasp.net-core-mvc

How to store Picture User Profile on Database Table AspNetUsers using ASP.NET Core MVC


I'm working on an ASP.NET Core 5 MVC web application. I face issue I can't store Picture User on database table [dbo].[AspNetUsers].

How do I store a personal picture of user on table [dbo].[AspNetUsers].

I already add new column PictureUser on table [dbo].[AspNetUsers] to store User Picture.

I need to know what to modify on the controller and view to store user picture on database table.

I'm working on Microsoft identity membership when registering users

my model is:

public class RegisterVM
{
    public string EmailAddress { get; set; }
    public string Password { get; set; }
    public byte[] PictureUser { get; set; }

}

my action controller:

[HttpPost]
public async Task<IActionResult> Register(RegisterVM registerVM)
{
   
    var newUser = new ApplicationUser()
    {
        FullName = registerVM.FullName,
        Email = registerVM.EmailAddress,
        UserName = registerVM.EmailAddress,
        //How to add Image upload Here
    };

    var newUserResponse = await _userManager.CreateAsync(newUser, registerVM.Password);
    if (newUserResponse.Succeeded)
    {
        await _signInManager.SignInAsync(newUser, isPersistent: false);
  
        return View("RegisterCompleted");
    }
   
    return View(registerVM); 
}

on view:

@model RegisterVM;

   <form asp-action="Register">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
      
        <div class="form-group">
            <label asp-for="EmailAddress" class="control-label"></label>
            <input asp-for="EmailAddress" class="form-control" />
            <span asp-validation-for="EmailAddress" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Password" class="control-label"></label>
            <input asp-for="Password" class="form-control" />
            <span asp-validation-for="Password" class="text-danger"></span>
        </div>
         //How to add Image upload Here

        <div class="form-group">
            <input class="btn btn-outline-success float-right" type="submit" value="Sign up" />
           
        </div>
    </form>
            

How can I add Image Upload on the View And Controller actions?


Solution

  • Inherit from the identityUser class and add the field related to the image. It is also better to save the address of the image.

    Public class user : identityuser{
    
     //add image property
    
       Public string imgUrl {set; get;}
    
    }