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

C# 如何从控制器内部将模型的值放入数据库表中?,c#,asp.net,asp.net-mvc,database,C#,Asp.net,Asp.net Mvc,Database,我正在努力学习ASP.NETMVC。我正在使用预填充的asp.net项目,该项目中有一个假的网站。我有一个表单,在提交时,我想把这些值放到数据库中的一个表中。我还想添加一个功能,如果数据库中已经存在电子邮件,我会将它们重定向到另一个页面 我添加了自己的视图、模型和控制器 这是我的数据库附带问题:我是否应该将创建的表放入DefaultConnection而不是Users.mdf 我的模型: public class RegisterLoyaltyViewModel { public str

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

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

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

我的模型:

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页。如果电子邮件不存在,只需将模型提交到数据库,并重定向到第y页。

您需要一个数据层实现来访问数据库,有多种方法,其中一种是使用实体框架,看看以获得一些想法,或者,您也可以使用连接到数据库。

创建控制器操作以获取RegisterRoyalty视图。该视图包含RegisterRoyalty表单的。将表格张贴到“登记处”暴力行动。然后,如果需要,您可以执行逻辑并将模型添加到数据库中

//
// 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>

您好@03Usr,我浏览了实体框架的指南,现在已经建立了数据库和上下文,但我仍然不确定如何从模型中获取数据并将其放入数据库本身。