アプリに問題があります。私はこの複数のモデルを持っています:
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: オブジェクト参照がオブジェクトのインスタンスに設定されていません。
これで私を助けてください。ありがとうございました。