簡體   English   中英

誰能幫我弄清楚為什么我的注冊表不會提交到我的數據庫?

[英]Can anyone help me figure out why my registration form will not submit to my database?

我正在嘗試為我的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屬性。 我想以后再試一下。 我不確定該屬性是否需要包含在數據庫中。

接下來是我的綁定視圖模型:

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對象以檢索用戶輸入,然后構建了對象本身。 我將所有用戶輸入收集到該對象中,如果它的有效數據我嘗試創建數據訪問層連接並將對象添加到其中。 在savechanges周圍(據我所知)是我遇到問題的地方(我認為??)。

我不知道這有多重要,但是這是我的觀點供參考:

<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的新手,因此非常感謝您的幫助。

您必須將模型添加到視圖中

@model Yourproject.Models.userInfo

我想到了。 基本上,我在其中設置了代碼以填充列表並在視圖中顯示它。 我刪除了所有這些內容,只是將表單帖子添加到userInfo對象,然后將其保存到數據庫。 一旦我走了幾個小時,那很容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM