我有一个AccountRegister模型,我在一个帐户模型中,然后将该帐户模型添加到数据库。单击“创建”按钮时,没有错误/异常,它只是重定向到索引,就像它已插入数据库一样,但它没有。
我使用AccountRegister模型的原因是我希望用户输入密码和电子邮件两次。
我希望有人可以帮忙解决这个问题。
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Account
{
public int Id { get; set; }
[Display(Name = "Level")]
public int LevelId { get; set; }
[Required]
[StringLength(50, MinimumLength = 2)]
//remote check
public string Username { get; set; }
[Required]
[StringLength(50, MinimumLength = 6)]
public string Password { get; set; }
[Required]
[StringLength(50, MinimumLength = 2)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[StringLength(50, MinimumLength = 2)]
[Display(Name = "Middle Name")]
public string MiddleName { get; set; }
[Required]
[StringLength(50, MinimumLength = 2)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[DataType(DataType.Date)]
public DateTime Birthday { get; set; }
[Display(Name = "Gender")]
public int GenderId { get; set; }
[Required]
[StringLength(50, MinimumLength = 2)]
public string Address { get; set; }
[Display(Name = "Zip Code")]
public Nullable<int> ZipCode { get; set; }
[Required]
[StringLength(50, MinimumLength = 2)]
public string City { get; set; }
[Display(Name = "Country")]
public Nullable<int> CountryId { get; set; }
public Nullable<int> Phone { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Display(Name = "Account Created")]
public DateTime Created { get; set; }
[Display(Name = "Account Last Updated")]
public Nullable<DateTime> Updated { get; set; }
[Display(Name = "Password Updated")]
public Nullable<DateTime> PasswordUpdated { get; set; }
public virtual Country Country { get; set; }
public virtual Gender Gender { get; set; }
public virtual Level Level { get; set; }
}
public class RegisterAccount
{
[Required]
[StringLength(50, MinimumLength = 2)]
//remote check
public string Username { get; set; }
[Required]
[StringLength(50, MinimumLength = 6)]
public string Password { get; set; }
[Required]
[CompareAttribute("Password", ErrorMessage = "Passwords don't match.")]
public string ConfirmPassword { get; set; }
[Required]
[StringLength(50, MinimumLength = 2)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[StringLength(50, MinimumLength = 2)]
[Display(Name = "Middle Name")]
public string MiddleName { get; set; }
[Required]
[StringLength(50, MinimumLength = 2)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[DataType(DataType.Date)]
public DateTime Birthday { get; set; }
public int GenderId { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[CompareAttribute("Email", ErrorMessage = "Emails don't match.")]
public string ConfirmEmail { get; set; }
public virtual Gender Gender { get; set; }
}
public ActionResult Register()
{
ViewBag.GenderId = new SelectList(db.Genders, "Id", "Name");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterAccount registerAccount)
{
if (ModelState.IsValid)
{
Account account = new Account()
{
LevelId = 1,
Username = registerAccount.Username,
Password = registerAccount.Password,
FirstName = registerAccount.FirstName,
MiddleName = registerAccount.MiddleName,
LastName = registerAccount.LastName,
GenderId = registerAccount.GenderId,
Email = registerAccount.Email,
Created = DateTime.Now
};
db.Accounts.Add(account);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.GenderId = new SelectList(db.Genders, "Id", "Name", registerAccount.GenderId);
return View(registerAccount);
}
CREATE TABLE [dbo].[Account] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[LevelId] INT NOT NULL,
[Username] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[FirstName] NVARCHAR (50) NOT NULL,
[MiddleName] NVARCHAR (50) NULL,
[LastName] NVARCHAR (50) NOT NULL,
[Birthday] DATE NOT NULL,
[GenderId] INT NOT NULL,
[Address] NVARCHAR (50) NULL,
[ZipCode] INT NULL,
[City] NVARCHAR (50) NULL,
[CountryId] INT NULL,
[Phone] INT NULL,
[Email] NVARCHAR (50) NOT NULL,
[Created] DATETIME NOT NULL,
[Updated] DATETIME NULL,
[PasswordUpdated] DATETIME NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
FOREIGN KEY ([LevelId]) REFERENCES [dbo].[Level] ([Id]),
FOREIGN KEY ([GenderId]) REFERENCES [dbo].[Gender] ([Id]),
FOREIGN KEY ([CountryId]) REFERENCES [dbo].[Country] ([Id])
);