任何人都可以帮我弄清楚为什么我的注册表单不会提交到我的数据库?

时间:2016-04-28 23:22:38

标签: sql asp.net html5 asp.net-mvc-5

我正在尝试为我的mvc5 asp.net应用程序创建一个注册表单。我似乎无法让它发挥作用。我正在使用MVC5构建一个asp.net应用程序,并使用SQLExpress 2014来构建我想要输入的数据库。我想我并不完全理解模型绑定和数据访问层。当我调试它似乎逐步通过我想要它的方式但最终值回到null。

型号:

public class userInfo
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int userID { get; set; }

        [Required(ErrorMessage = "First name is required")]
        public string firstName { get; set; }

        [Required(ErrorMessage = "Last name is required")]
        public string lastName { get; set; }

        [Required(ErrorMessage = "Email is required")]
        [EmailAddress]
        public string email { get; set; }

        [Required(ErrorMessage = "Username is required")]
        public string userName { get; set; }

        [Required(ErrorMessage = "Password is required")]
        [DataType(DataType.Password)]
        public string password { get; set; }

        /*[NotMapped]
        [Compare("Password", ErrorMessage = "Please confirm your password")]
        [DataType(DataType.Password)]
        public string confirmPassword { get; set; }*/
    }

上面是我的userInfo模型。我已经注释掉了comparePassword属性。我想我稍后会尝试解决这个问题。我不确定该属性是否需要包含在数据库中。

接下来是我的用于绑定的viewmodel:

public DbSet<userInfo> Users { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<userInfo>().ToTable("tblusers");
    }

在我的accountcontroller中,我尝试从表单中检索数据并将其发布到数据库表中:

public ActionResult Register()
{
    return View();
}

[HttpPost]
public ActionResult Register(FormCollection userBuild)
{
    userInfoViewModel vm = new userInfoViewModel();

    userInfo user = new userInfo();
    user.firstName = Request.Form["firstName"];
    user.lastName = Request.Form["lastName"];
    user.email = Request.Form["email"];
    user.userName = Request.Form["userName"];
    user.password = Request.Form["password"];

    if (ModelState.IsValid)
    {
        try
        {
            userInfoDal Dal = new userInfoDal();
            Dal.Users.Add(user);
            Dal.SaveChanges();
            vm.newUser = new userInfo();
        }
        catch
        {
            return View("Register");
        }
    }
    else
    {
        vm.newUser = user;
    }
    userInfoDal dal = new userInfoDal();             
    return View("Register");
}

如您所见,我构建了一个viewmodel对象以便检索用户输入,然后我自己构建对象。我将所有用户输入收集到对象中,如果有效数据我尝试创建数据访问层连接并将对象添加到它。保存更改(从我可以看出)是我遇到问题的地方(我认为??)。

我不知道这有多重要,但这是我的观点供参考:

<h2>Registration</h2>

@using (Html.BeginForm("Register", "Account", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>New User Registration</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.userName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.userName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.userName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.firstName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.firstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.firstName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.lastName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.lastName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.lastName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
            </div>
        </div>

        @*<div class="form-group">
            @Html.LabelFor(model => model.confirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.confirmPassword, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.confirmPassword, "", new { @class = "text-danger" })
            </div>
        </div>-->*@

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input id="Submit1" type="submit" value="Register" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

然后viewModel中没有任何内容,除了:

public userInfo newUser { get; set; }

我已经用这种方式将我的大脑震撼了几天。我对MVC很新,所以对此的任何帮助都将非常感激。

2 个答案:

答案 0 :(得分:0)

您必须在视图中添加模型

@model Yourproject.Models.userInfo

答案 1 :(得分:0)

我明白了。基本上我在那里设置代码来填充列表并在视图中显示它。我删除了所有这些,只是将表单帖子添加到userInfo对象,然后将其保存到DB。一旦我离开了几个小时,这很容易。