0

I have an AccountRegister model that i make into an Account model and then add that Account model to the database. There is zero errors/exceptions when i click the create button, it just redirects to index as if it has inserted into the database, but it has not.

The reason why i use AccountRegister model is that i want the user to enter password and email twice.

I hope someone can help with this.

Model class:

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

Controller method:

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

Database table:

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])
);
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
DrexxDK
  • 16
  • 3
  • can you show us some HTML...where you submit this model – A.T. Dec 02 '13 at 05:00
  • I cant link it the next 7 hours. I'm new here so it wont allow that, but i can tell you that it is 100% auto generated with scaffolding. havent changed anything in it. – DrexxDK Dec 02 '13 at 06:10
  • I figured out what the problem was. I had [Required] on 2 fields in Account. Very stupid mistake. Sucks to be noob :P – DrexxDK Dec 02 '13 at 06:49

0 Answers0