2

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

enter image description here

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.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Nash
  • 65
  • 5
  • Under no circumstances do you ever set the `value` attribute when using `TagHelpers` - you set the value of the property in the GET method before you pass the model to the view. And the error is because `@ViewBag.User.FirstName` is `null` - you did not set it in the POST method. –  Feb 10 '18 at 02:41
  • @StephenMuecke thank you for answer, but how can I put `@ViewBag.User.FirstName` to POST method, if I need that info at the moment of page rendering? – Nash Feb 10 '18 at 02:45
  • Read the first comment - your DONT! - You pass the model to the view and remove the `value` attribute. `return View("Settings", exists);` etc (but `exists` does not make sense as a variable name - use something appropriate like `User user = _context.Users.Where(...)` and then you should be checking if its `null` before calling `if(user.Status != "Admin")` since that will also throw an exception if it is. –  Feb 10 '18 at 02:48
  • And the fact your have `int? id = HttpContext.Session.GetInt32("userId");` in a POST method means you have far more serious issues. Go to the MVC site and work through the tutorials, especially those on Security, Authorization and Authentication. –  Feb 10 '18 at 02:55
  • @StephenMuecke trying to `return View("Settings", exists)` brought me to _InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'sellwalker.Models.User', but this ViewDataDictionary instance requires a model item of type 'sellwalker.Models.Register'._ I don't quiet understand what do you mean in 2nd comment. Do I need ViewBag there still? – Nash Feb 10 '18 at 03:08
  • Then read [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) to understand why that happens - your view expects a model which is typeof `Register` but you have returned typeof `User` - you need to return an instance of `Register` - i.e. `return View("Settings", check);` –  Feb 10 '18 at 03:11

0 Answers0