簡體   English   中英

使用實體框架在數據庫中插入記錄的問題

[英]Issue With Insert Record In Database with Entity Framework

我在 SQL Server 中有表,然后轉到 Visual Studio -> 服務器資源管理器 -> 添加連接,我的表添加成功。

問題是當我按下創建按鈕時數據沒有插入到 SQL Server 中,然后沒有顯示任何錯誤,我認為記錄已創建,但是當我在 SQL Server Management Studio 中刷新時,在表中沒有顯示記錄...

表: empls

empid int primary key
empname varchar(50)
empsalary varchar(50)
empage int

班級:

[Table("empls")]
public class stdimg
{
    [Key]
    public int empid { get; set; }

    public string empname { get; set; }
    public string empsalary { get; set; }
    public int empage { get; set; }
}

Context 類是數據庫和應用程序之間的橋梁:

using System.Data.Entity;

public class studcontext:DbContext
{
    public DbSet<stdimg> stds { get; set; }
}

家庭控制器:

public class HomeController : Controller
{
    studcontext _context = new studcontext();

    [HttpGet]
    public ActionResult InsertData()
    {
        return View();
    }

    [HttpPost]
    public ActionResult InsertData(stdimg studs)
    {
        _context.stds.Add(studs);
        _context.SaveChanges();
        ViewBag.message = "data inserted successfully"; 
        return View();
    }
}

插入數據.cshtml:

@model ImageUploadUsingMvc.Models.stdimg

@{
    ViewBag.Title = "InsertData";
}

<h2>InsertData</h2>

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

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

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

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

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

我已經在 SQL Server Management Studio 中創建了一個數據庫,並成功地在我的項目中添加了一個數據庫。 但是當我點擊創建按鈕時,代碼沒有產生任何錯誤,但是刷新我的數據庫后,記錄沒有顯示......

我把viewbag.message="record successfully"消息不顯示,記錄不顯示在 SQL Server 中?

有人可以幫忙嗎? 我究竟做錯了什么?

您不在 Html.BeginForm() 上執行 Post 那么我建議您為該工作創建獨立的服務

暫無
暫無

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

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