简体   繁体   English

CRUD操作以插入数据

[英]CRUD operation to insert data

My program compiles with no errors but unfortunately it does not insert the data into the database. 我的程序编译没有错误,但是不幸的是它没有将数据插入数据库。 the database table is called "Customers" Any help would be great here is my code in my model i have: 数据库表被称为“客户”。任何帮助都会很棒,这是我在模型中的代码:

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

    //
    // POST: /Default1/Create

    [HttpPost]
    public ActionResult Apply(CUSTOMER customer)
    {
        if (ModelState.IsValid)
            return View();
        try
        {
            db.CUSTOMERs.Add(customer);
            db.SaveChanges();
            return RedirectToAction("Apply");
        }
        catch
        {
            return View();
        }
    }

then at my mvc i have: 然后在我的MVC我有:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<GreenEnergyGr.Models.CUSTOMER>" %>

Apply 应用

<form id="form1" runat="server">

Apply 应用

<fieldset>
    <legend>CUSTOMER</legend>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.CustomerName) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.CustomerName) %>
        <%: Html.ValidationMessageFor(model => model.CustomerName) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.CustomerSurname) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.CustomerSurname) %>
        <%: Html.ValidationMessageFor(model => model.CustomerSurname) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.CustomerAddress) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.CustomerAddress) %>
        <%: Html.ValidationMessageFor(model => model.CustomerAddress) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.CustomerLocation) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.CustomerLocation) %>
        <%: Html.ValidationMessageFor(model => model.CustomerLocation) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.CustomerTelephone) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.CustomerTelephone) %>
        <%: Html.ValidationMessageFor(model => model.CustomerTelephone) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.CustomerEmail) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.CustomerEmail) %>
        <%: Html.ValidationMessageFor(model => model.CustomerEmail) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.CustomerPostCode) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.CustomerPostCode) %>
        <%: Html.ValidationMessageFor(model => model.CustomerPostCode) %>
    </div>

    <p>
        <input type="submit" value="Send" />
    </p>
</fieldset>

If your view is valid then you have configured you probably did save data just not to the database you are looking at. 如果您的视图有效,则说明您已配置了数据,只是没有将数据保存到正在查看的数据库中。 If you have not properly specified the name in the right place entity framework will extract a name. 如果未正确指定名称,则在正确的位置实体框架将提取名称。 Look at the other databases on the server and see if there is a rogue. 查看服务器上的其他数据库,看看是否有恶意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM