ASP.NET MVC Nullreferenceexception: System.Web.Mvc.WebViewPage<TModel>.Model.get.<Field at offset 0x00000004> 为空

ASP.NET MVC Nullreferenceexception: System.Web.Mvc.WebViewPage<TModel>.Model.get.<Field at offset 0x00000004> was null

我正在为学校开发一个项目 (ASP.NET MVC),但今天我 运行 遇到了这个错误。我已经尝试修复此问题几个小时,但没有任何效果。 在此应用程序中,您登录并加载您的个人资料。这行得通,但是一旦我更改了配置文件数据(以一种形式),post 它,以及 return 具有刷新视图模型的视图(这仍然可以正常工作),我就会得到这个 nullreferenceexception。我一直在查看此异常详细信息,但无法弄清楚什么是 null,以及导致此异常的原因。

这是我的观点:

@model SailyWebApp.Viewmodels.HomeUserViewmodel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<body>
    @if(Session["User"] != null && !string.IsNullOrEmpty(Convert.ToString(Session["User"])))
     {
    <h2>Profile of @Session["User"].ToString()</h2>
    <form runat="server" method="post" id="userdata">
        Name:<br>
        <input type="text" name="Name" value="@Model.user.UserName" required /><br>
        Adress:<br>
        <input type="text" name="Address" value="@Model.user.Adress" required /><br>
        City:<br>
        <input type="text" name="City" value="@Model.user.City" required /><br>
        E-mail:<br>
        <input type="text" name="Mail" value="@Model.user.MailAdress" required /><br>
        Phone number:<br>
        <input type="text" name="Phonenumber" value="@Model.user.PhoneNumber" required /><br>
        Profile description:<br>
        <input type="text" name="Description" value="@Model.user.Description" required /><br>
        <input type="submit" name="Save" value="Save" />
    </form>
    }
    else{
    <p>Please log in</p>
    }
</body>

我的 HomeController 看起来像这样:

public ActionResult Index()
    {
        if (Session["User"] != null)
        {
            User currentUser = (User)Session["User"];
            HomeUserViewmodel viewmodel = new HomeUserViewmodel();
            viewmodel.user = currentUser;
            return View(viewmodel);
        }

        return View();
    }
    [HttpPost]
    public ActionResult Index(HomeUserViewmodel viewmodel)
    {
        User currentUser = (User)Session["User"];
        if (Logic.UserLogic.EditProfile(viewmodel.Address, viewmodel.City, viewmodel.Mail, viewmodel.Phonenumber, viewmodel.Description, currentUser.UserId))
        {
            currentUser = Logic.UserLogic.LoadUser(currentUser.UserId);
            HomeUserViewmodel newviewmodel = new HomeUserViewmodel();
            viewmodel.user = currentUser;
            Session.Clear();
            Session["User"] = currentUser;
            return View(newviewmodel);
        }
        else
        {
            return View();
        }
    } 

我的 HomeUserView 模型

public class HomeUserViewmodel
{
    public User user;

    private string name;
    private string address;
    private string city;
    private string mail;
    private string phonenumber;
    private string description;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public string Address
    {
        get { return address; }
        set { address = value; }
    }
    public string City
    {
        get { return city; }
        set { city = value; }
    }
    public string Mail
    {
        get { return mail; }
        set { mail = value; }
    }
    public string Phonenumber
    {
        get { return phonenumber; }
        set { phonenumber = value; }
    }
    public string Description
    {
        get { return description; }
        set { description = value; }
    }
}

异常详情:

System.NullReferenceException: 'object reference not set to an instance of an object'

System.Web.Mvc.WebViewPage.Model.get。为空。

在此先感谢,我真的不知道在哪里可以找到这个空值

if (Logic.UserLogic.EditProfile(viewmodel.Address, viewmodel.City, viewmodel.Mail, viewmodel.Phonenumber, viewmodel.Description, currentUser.UserId))
{
    currentUser = Logic.UserLogic.LoadUser(currentUser.UserId);
    HomeUserViewmodel newviewmodel = new HomeUserViewmodel();
    viewmodel.user = currentUser; // <- It's right here, this is your problem
    Session.Clear();
    Session["User"] = currentUser;
    return View(newviewmodel);
}

您在错误的对象上填充了用户字段,您发送到视图的视图模型有一个空字段,因此您抛出了一个空引用异常。