Having a strange issue trying to update user info. I passing TempData through inputs' Value here. But getting a strange error then An unhandled exception occurred while processing the request.
RunTimeBinderException: Cannot perform runtime binding on a null reference
Could you look at the code?
[HttpPost]
[Route("/update_user")]
public async Task<IActionResult> UpdateInfo(Register check)
{
int? id = HttpContext.Session.GetInt32("userId");
if(id == null)
{
return RedirectToAction("LoginPage", "User");
}
else
{
User thisUser = _context.Users.Where(u=>u.UserId == id).SingleOrDefault();
if(ModelState.IsValid)
{
thisUser.FirstName = check.FirstName;
thisUser.LastName = check.LastName;
thisUser.Email = check.Email;
// thisUser.Password = check.Password;
var uploadDestination = Path.Combine(_hostingEnvironment.WebRootPath, "uploaded_images");
if (check.ProfileImage == null)
{
_context.SaveChanges();
if(thisUser.Status != "Admin")
{
return View("Settings");
}
else
{
return View("AdminSettings");
}
}
else
{
var filepath = Path.Combine(uploadDestination, check.ProfileImage.FileName);
using (var fileStream = new FileStream(filepath, FileMode.Create))
{
await check.ProfileImage.CopyToAsync(fileStream);
thisUser.ProfilePic = "/uploaded_images/" + check.ProfileImage.FileName;
}
_context.SaveChanges();
if(thisUser.Status != "Admin")
{
return View("Settings");
}
else
{
return View("AdminSettings");
}
}
}
else
{
if(thisUser.Status != "Admin")
{
return View("Settings");
}
else
{
return View("AdminSettings");
}
}
}
}
HTML -
<label asp-for="FirstName">First Name</label>
<input asp-for="FirstName" class="form-control" id="FirstName" value="@ViewBag.User.FirstName">
<span asp-validation-for="FirstName"></span>
<br>
<label asp-for="LastName">Last Name</label>
<input asp-for="LastName" class="form-control" id="LastName" value="@ViewBag.User.LastName">
<span asp-validation-for="LastName"></span>
Rendering page method:
[HttpGet]
[Route("/settings")]
public IActionResult Settings()
{
int? id = HttpContext.Session.GetInt32("userId");
if(id == null)
{
return RedirectToAction("LoginPage", "User");
}
else
{
User exists = _context.Users.Where(u=>u.UserId == id).SingleOrDefault();
ViewBag.User = exists;
if(exists.Status != "Admin")
{
return View("Settings");
}
else
{
return View("AdminSettings");
}
}
}
Any thoughts about it? Don't even know where should I start. ViewBag data shows up correctly in the field, just not passing through the form somehow.