0

我有一个 AccountRegister 模型,我将它制作成一个 Account 模型,然后将该 Account 模型添加到数据库中。当我单击创建按钮时,错误/异常为零,它只是重定向到索引,就好像它已插入数据库一样,但它没有。

我使用 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])
);
4

0 回答 0