如何从控制器内部将模型的值放入数据库表?

时间:2015-10-11 16:51:49

标签: c# asp.net asp.net-mvc database

我正在尝试学习ASP.net MVC。我正在使用预先填充的asp.net项目,其中包含一个假网站。我有一个表单,在提交时,我想将值放入我的数据库中的表中。我还想添加一些功能,如果电子邮件已经存在于数据库中,我会将它们重定向到另一个页面。

我添加了自己的视图,模型和控制器。

这是我的数据库(附带问题:我应该将我创建的表放入DefaultConnection而不是Users.mdf吗?)

enter image description here

我的模特:

public class RegisterLoyaltyViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public string Password { get; set; }
    public string Phone { get; set; }
    public string SecurityQuestion { get; set; }
    public string SecurityAnswer { get; set; }
    public string optSweepstakes { get; set; }
    public string optEmails { get; set; }
}

我的相关控制器代码:

 // POST: /Account/RegisterLoyalty
    [HttpPost]
    [AllowAnonymous]
    public void RegisterLoyalty(RegisterLoyaltyViewModel model)
    {
        //Currently nothing here
    }

我认为我需要做的就是点击控制器内的数据库。我需要检查表中是否已存在电子邮件,如果已存在,则重定向到第x页。如果电子邮件不存在,只需将model提交到数据库,然后重定向到第y页。

2 个答案:

答案 0 :(得分:0)

您需要一个数据层实现来访问数据库,有多种方法可以使用实体框架,查看here以获取一些想法,或者您可以使用{{ 3}}连接数据库。

答案 1 :(得分:0)

创建控制器操作以获取RegisterLoyalty视图。该视图包含RegisterLoyalty表单。将表单POST发送到RegisterLoyalty操作。然后,您可以执行逻辑并在必要时将模型添加到数据库中。

//
// GET: /Account/RegisterLoyalty
[AllowAnonymous]
public ActionResult RegisterLoyalty()
{
    return View();
}

//
// POST: /Account/RegisterLoyalty
[HttpPost]
[AllowAnonymous]
public ActionResult RegisterLoyalty(RegisterLoyaltyViewModel model)
{
    var db = new AccountLoyaltyDbContext();

    var emailExists = db.Loyalties.Any(x => x.EmailAddress == model.EmailAddress);

    if (emailExists)
    {
        return RedirectToAction("X");
    }

    db.Loyalties.Add(model);
    db.SaveChanges();

    return RedirectToAction("Y");
}

//
// GET: /Account/X
[AllowAnonymous]
public ActionResult X()
{
    return View();
}

//
// GET: /Account/Y
[AllowAnonymous]
public ActionResult Y()
{
    return View();
}

RegisterLoyalty.cshtml

@model UserLoyalty.Models.RegisterLoyaltyViewModel

@{
    ViewBag.Title = "RegisterLoyalty";
}

<h2>RegisterLoyalty</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>RegisterLoyaltyViewModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <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.EmailAddress, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailAddress, "", 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.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

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

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

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

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