从C#中的输入值中获取信息时出错

时间:2018-02-10 02:33:37

标签: c# asp.net-mvc entity-framework asp.net-core-mvc

尝试更新用户信息时遇到一个奇怪的问题。我在这里通过输入值传递TempData。但是得到一个奇怪的错误然后处理请求时发生了未处理的异常。

  

RunTimeBinderException:无法对空引用执行运行时绑定

enter image description here

你能看一下代码吗?

[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>   

渲染页面方法:

[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");
        }
    }
}

有什么想法吗?甚至不知道我应该从哪里开始。 ViewBag数据在字段中正确显示,只是不以某种方式通过表单。

0 个答案:

没有答案