Asp.net mvc 4 ASP.NET MVC 4,视图中的多个模型,System.NullReferenceException:

Asp.net mvc 4 ASP.NET MVC 4,视图中的多个模型,System.NullReferenceException:,asp.net-mvc-4,nullreferenceexception,Asp.net Mvc 4,Nullreferenceexception,我的应用程序有问题。 我有这个多重模型: public class MultipleModel { public Staff Staff; public RegisterModel RegisterModel; } public class RegisterModel { [Required] [Display(Name = "User name")] public string UserName { get; set; } [Required

我的应用程序有问题。 我有这个多重模型:

public class MultipleModel
{
    public Staff Staff;
    public RegisterModel RegisterModel;
}

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}


public partial class Staff
{
    public Staff()
    {
        this.Tables = new HashSet<Tables>();
    }

    public int EmployeeID { get; set; }
    public string First_name { get; set; }
    public string Last_name { get; set; }
    public string Telephone { get; set; }
    public string Description { get; set; }
    public string Username { get; set; }

    public virtual ICollection<Tables> Tables { get; set; }
}
这一观点是:

@model test13.Models.MultipleModel
@{
ViewBag.Title = "MultipleModelView";
}

<h2>MultipleModelView</h2>
@using (Html.BeginForm("MultipleModelView", "Account", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
    <legend>Registration Form</legend>
    <ol>
        <li>
            @Html.LabelFor(m => m.RegisterModel.UserName)
            @Html.TextBoxFor(m => m.RegisterModel.UserName)
        </li>            
        <li>
            @Html.LabelFor(m => m.RegisterModel.Password)
            @Html.PasswordFor(m => m.RegisterModel.Password)
        </li>
        <li>
            @Html.LabelFor(m => m.RegisterModel.ConfirmPassword)
            @Html.PasswordFor(m => m.RegisterModel.ConfirmPassword)
        </li>
        <li>
            @Html.LabelFor(m => m.Staff.First_name)
            @Html.TextBoxFor(m => m.Staff.First_name)
        </li>
        <li>
            @Html.LabelFor(m => m.Staff.Last_name)
            @Html.TextBoxFor(m => m.Staff.Last_name)
        </li>
        <li>
            @Html.LabelFor(m => m.Staff.Telephone)
            @Html.TextBoxFor(m => m.Staff.Telephone)
        </li>

    </ol>
    <input type="submit" value="Register" />
</fieldset>

}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
System.NullReferenceException:对象引用未设置为对象的实例


帮我拿这个。谢谢。

初始化构造函数中的注册模型

public class MultipleModel
{
    public MultipleModel()
    {
       this.RegisterModel = new RegisterModel();
    }
    public Staff Staff{get;set;};
    public RegisterModel RegisterModel{get;set;};
}

初始化构造函数中的寄存器模型

public class MultipleModel
{
    public MultipleModel()
    {
       this.RegisterModel = new RegisterModel();
    }
    public Staff Staff{get;set;};
    public RegisterModel RegisterModel{get;set;};
}
在视图模型中使用而不是使用:

public class MultipleModel
{
    public Staff Staff { get; set; }
    public RegisterModel RegisterModel { get; set; }
}
ASP.NET MVC中的默认模型绑定器仅适用于属性。字段被忽略。

使用而不在视图模型中:

public class MultipleModel
{
    public Staff Staff { get; set; }
    public RegisterModel RegisterModel { get; set; }
}

ASP.NET MVC中的默认模型绑定器仅适用于属性。字段被忽略。

就是这样。非常感谢,祝你今天愉快。就这样。非常感谢,祝你今天愉快。