ASP.NET MVC 4,View中的多个模型,System.NullReferenceException:

时间:2013-03-28 10:40:48

标签: asp.net-mvc-4 nullreferenceexception

我的App有问题。 我有这个多模型:

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; }
}

控制器是:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult MultipleModelView(MultipleModel model)
    {          
        if (ModelState.IsValid)
        {
            try
            {                   
                WebSecurity.CreateUserAndAccount(model.RegisterModel.UserName, model.RegisterModel.Password);
                Roles.AddUserToRole(model.RegisterModel.UserName, "Customer");
                Staff staff = new Staff();

                staff.Username = model.RegisterModel.UserName;
                staff.First_name = model.Staff.First_name;
                staff.Last_name = model.Staff.Last_name;
                staff.Telephone = model.Staff.Telephone;
                db.Staff.Add(staff);
                db.SaveChanges();

                WebSecurity.Login(model.RegisterModel.UserName, model.RegisterModel.Password);

                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }
        return View(model);
    }

观点是:

@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")
}

当我将数据填充到TextBox并发布它们时,App在这部分代码中得到了Exception:

 WebSecurity.CreateUserAndAccount(model.RegisterModel.UserName, model.RegisterModel.Password);

System.NullReferenceException:未将对象引用设置为对象的实例。

请帮我解决这个问题。谢谢。

2 个答案:

答案 0 :(得分:1)

在视图模型中使用properties而不是fields

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

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

答案 1 :(得分:0)

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

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