1

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

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

当我将数据填充到文本框并发布它们时,应用程序在这部分代码中让我出现异常:

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

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

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

4

2 回答 2

1

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

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

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

于 2013-03-28T11:06:17.897 回答
0

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

public class MultipleModel
{
    public MultipleModel()
    {
       this.RegisterModel = new RegisterModel();
    }
    public Staff Staff{get;set;};
    public RegisterModel RegisterModel{get;set;};
}
于 2013-03-28T10:51:11.737 回答