无法将值从表单保存到数据库

时间:2018-03-24 00:32:49

标签: asp.net-mvc entity-framework model ef-code-first

我正在尝试用Entity Framework编写我的第一个ASP.NET MVC应用程序(代码优先)。

我正在尝试将数据从表单插入到数据库中的特定表中,但它不起作用。当我按"提交"按钮,页面刷新但表格仍然没有任何价值。页面或控制台中没有错误。

你能看看代码并帮助我吗?

以下是我的代码:

查看:

@model VeterinaryApp.Models.Clients

@{
    ViewBag.Title = "AddClient";
    Layout = "~/Views/SharedViews/_MainLayout.cshtml";
}

<h2>AddClient</h2>

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Surname, "", 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.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">
            <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>

控制器:

public class AddClientController : Controller
{
    // GET: AddClient
    public ActionResult AddClient()
    {
        return View();
    }

    //POST
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ClientsId, Name, Surname, Email, Phone")] Clients clients)
    {
        if (ModelState.IsValid)
        {
            using (StoreContext db = new StoreContext()) //DbContext
            {
                db.Clients.Add(clients);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }

        return View(clients);
    }
}

模型

 public class Clients
    {
        public int ClientsId { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }
        public int Phone { get; set; }

        public virtual ICollection<BookVisit> BookedVisits { get; set; }
        public virtual ICollection<Animals> OwnedAnimals { get; set; }
    }

0 个答案:

没有答案