TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Hadeel e
NA
50
6.6k
Update user profile in MVC 5
Apr 28 2020 10:14 PM
Hi, I am using identitiy framework to my existing application (I added it after I start working with the login).
Generally, I was using my own database. Right Now, I have the user inserted in 2 tables 1- my own table 2- aspnetUsers table.
As a resule, when I try to update or display user profile I use the identity user to generate that information.
However, when the user login and try to access the profile view, I'm getting an error that the "UserName" parameter cann't be null.
How Would I generate the user information in this case?
My action function looks as the following:
[HttpGet]
public
async Task <ActionResult> UpdateDriverProfile()
{
var user = User.Identity.Name;
var userStore =
new
UserStore<IdentityUser>();
var userManager =
new
UserManager<IdentityUser>(
new
UserStore<IdentityUser>(
new
RidesDbContext()));
DriversModel driver =
new
DriversModel();
IdentityUser theUser =
new
IdentityUser()
{
UserName = driver.email,
Email = driver.email
};
driver.email = theUser.UserName;
driver.email = theUser.Email;
driver.phoneNumber = theUser.PhoneNumber;
driver.PasswordHash = theUser.PasswordHash;
driver.userID = theUser.Id;
var store = await userStore.FindByNameAsync(theUser.UserName);
theUser = await userManager.FindByNameAsync(driver.email);
DriversTier tier =
new
DriversTier();
driver = tier.getDriverByUserID(driver.userID);
return
View(
"UpdateDriverProfile"
, driver);
}
just to make things clear, this is how I rwgister or add the user to the database:
[HttpPost]
public
async Task<ActionResult> DriversRegistration(DriversModel driver, IEnumerable<HttpPostedFileBase> images)
{
var profileImg = images.ElementAt(0);
var licenseImg = images.ElementAt(1);
//First check the first Image
if
(profileImg !=
null
&& profileImg.ContentLength > 0)
{
driver.profileImgType = profileImg.ContentType;
string
fileName = profileImg.FileName;
string
pattern = @
"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"
;
Regex rgx =
new
Regex(pattern);
//Get the length of the image in bytes and create that array
driver.profileImg =
new
byte
[profileImg.ContentLength];
//fill the array of bytes with the image data
profileImg.InputStream.Read(driver.profileImg, 0, profileImg.ContentLength);
//Need to do model state checking here, provided some code for that already
ModelState.Clear();
}
//Now check the second Image
if
(licenseImg !=
null
&& licenseImg.ContentLength > 0)
{
driver.licenseImgType = licenseImg.ContentType;
string
fileName = licenseImg.FileName;
string
pattern = @
"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"
;
Regex rgx =
new
Regex(pattern);
//Get the length of the image in bytes and create that array
driver.licenseImg =
new
byte
[licenseImg.ContentLength];
//fill the array of bytes with the image data
licenseImg.InputStream.Read(driver.licenseImg, 0, licenseImg.ContentLength);
//Need to do model state checking here, provided some code for that already
ModelState.Clear();
}
if
(ModelState.IsValid)
{
bool
processError =
false
;
var userStore =
new
UserStore<IdentityUser>();
var userManager =
new
UserManager<IdentityUser>(userStore);
string
statusMessage =
""
;
IdentityUser theUser =
new
IdentityUser() {
UserName = driver.email,
Email = driver.email,
PasswordHash = driver.PasswordHash,
PhoneNumber = driver.phoneNumber
};
IdentityResult theResult = await userManager.CreateAsync(theUser,driver.PasswordHash);
var store = await userStore.FindByNameAsync(theUser.UserName);
theUser = await userManager.FindByNameAsync(driver.email);
string
UserName = theUser.UserName;
string
password = theUser.PasswordHash;
string
Email = theUser.Email;
string
phoneNumber = theUser.PhoneNumber;
string
userID = theUser.Id;
driver.userID = userID;
DriversTier tier =
new
DriversTier();
int
newID = tier.insertDriver(driver);
driver.driverId = newID;
if
(theResult == IdentityResult.Success)
{
//First check to see if User Role exists. If not create it and add user to User role.
if
(driver !=
null
)
{
var roleStore =
new
RoleStore<IdentityRole>();
var roleManager =
new
RoleManager<IdentityRole>(roleStore);
var therole =
new
IdentityRole(
"Driver"
);
theResult = await roleManager.CreateAsync(therole);
if
(theResult ==
null
)
{
statusMessage =
string
.Format(
"User Group Creation failed because : {0}"
, theResult.Errors.FirstOrDefault());
//Need to exit nicely here for some reason, we could not create the role with the DB
processError =
true
;
}
}
if
(!processError)
{
//The role exists, now add the user to the role
theResult = await userManager.AddToRoleAsync(theUser.Id,
"Driver"
);
statusMessage =
string
.Format(
"Identity User {0} was created successfully!<br /> {0} was added to the User group: {1}"
, theUser.UserName, theResult.Errors.FirstOrDefault());
}
}
else
{
//could not create a user
statusMessage =
string
.Format(
"User Creation failed because : {0}"
, theResult.Errors.FirstOrDefault());
processError =
true
;
}
List<IdentityUser> userList = userManager.Users.ToList<IdentityUser>();
return
RedirectToAction(
"ThankYou"
,
"Default"
);
}
else
{
return
View();
}
and this is the model I'm using for both registration and the profile:
public
class
DriversModel
{
[Key]
[Display(Name =
"Id"
)]
public
int
driverId {
get
;
set
; }
//this is the FK associated with the aspnetusers table
public
string
userID {
get
;
set
; }
[Display(Name =
"First Name"
)]
[Required(ErrorMessage =
"Pleas Enter Your First Name"
)]
public
string
firstName {
get
;
set
; }
[Display(Name =
"Last Name"
)]
[Required(ErrorMessage =
"Pleas Enter Your Last Name"
)]
public
string
lastName {
get
;
set
; }
[Display(Name =
"Email Address"
)]
[DataType(DataType.EmailAddress)]
[Required(ErrorMessage =
"Pleas Enter Your Email Address"
)]
[RegularExpression(
".+\\@.+\\..+"
, ErrorMessage =
"Please Enater a Valid Email Address"
)]
public
string
email {
get
;
set
; }
[Display(Name =
"Mobile Number"
)]
[Required(ErrorMessage =
"Pleas Enter Your Mobile Number"
)]
public
string
phoneNumber {
get
;
set
; }
[Display(Name =
"Address"
)]
[Required(ErrorMessage =
"Pleas Enter Your Address"
)]
public
string
Address {
get
;
set
; }
[Display(Name =
"City"
)]
[Required(ErrorMessage =
"Pleas Enter Your City"
)]
public
string
city {
get
;
set
; }
[Display(Name =
"State"
)]
[Required(ErrorMessage =
"Pleas Enter Your state"
)]
public
string
state {
get
;
set
; }
[Display(Name =
"Car"
)]
[Required(ErrorMessage =
"Please Identify Your Car"
)]
public
string
car {
get
;
set
; }
[Display(Name =
"Driver's License"
)]
[Required(ErrorMessage =
"Please Enter Your Driver's Licende Number"
)]
public
string
driverslicense {
get
;
set
; }
[Display(Name =
"Profile Image"
)]
[Required]
public
byte
[] profileImg {
get
;
set
; }
public
string
profileImgType {
get
;
set
; }
[Display(Name =
"License Image"
)]
[Required]
public
byte
[] licenseImg {
get
;
set
; }
public
string
licenseImgType {
get
;
set
; }
[Display(Name =
"Password"
)]
[DataType(DataType.Password)]
[Required(ErrorMessage =
"Please Enter a password"
)]
public
string
PasswordHash {
get
;
set
; }
[Display(Name =
"Username"
)]
public
string
UserName {
get
;
set
; }
}
}
I would appreciate your help. Thank You.
Reply
Answers (
7
)
help me for this message
tagging issue, In btwn 2 keywords i am trying to give comma