I have a problem in EditUserProfile in razor page, so there is no problem showing the user information in the page, but when I press edit button, I get user information through the form but I can’t save or user it.
The code
The page
I even tried to save the username in variable but here is what happens
userName PassedBy Form
trying to save it
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Firstly,your _User.UserName =”saeed_01″,and username is null in your picture,this is because this line of code has not been executed yet。When you excute it and go to the next line of code,you can see the username is “saeed_01”.
And if you want to save the edited user.I think you can change your Save in UserRepository like this:
public async Task Save(User user)
{
try
{
_context.Update(user);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
throw;
}
}
and change
_userRepositiory.Save();
to
await _userRepositiory.Save(user);
Update:
here is the endpoint in your code:

you try to change the endpoint to here:

And then you can see the username is not null
Here is a demo:
User:
public class User {
public int UserId { get; set; }
public string UserName { get; set; }
}
Action:
public IActionResult Index()
{
User _User = new User { UserId = 1, UserName = "sss" };
var username = _User.UserName;
return View();
}
Method 2
It could be possible that _User and var user in the method is pointing to the same object in memory. I can’t be sure because not all code is visible.
It all depends on the code in the userRepository. The ORM you are using could have some kind of cache and this could cause some weird behavior.
In the OnGet method clone the user, that you get from GetUserByUserId to the _User property.
public void OnGet()
{
var user = _userRepositiory.GetUserByUserName(HttpContext.User.GetUserId());
_User = new User(){
// set all properties here
};
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0




