首页 > 解决方案 > oracle 11g添加记录时显示成功但没有添加记录,返回空白详情页

问题描述

我是 ASP.Net MVC C# 的新手。我正在尝试使用 ASP.Net MVC C# 和存储过程向 Oracle 11g 数据库添加新记录。但它没有添加,当我填写表格并点击提交时,它说添加成功,但是当我检查数据库时,记录不存在。

我浏览了多个站点并使用了他们提供的示例中的模式,但仍然没有解决方案。代码和数据访问层似乎都在工作,应用程序没有给出任何错误。

数据访问层:

public string Insert(Participant participant)
{
    string newParticipant = "";

    try
    {
        oracleCommand = new OracleCommand("InsertProcedures.register", oracleConnection);
        oracleCommand.CommandType = CommandType.StoredProcedure;

        oracleCommand.Parameters.Add(new OracleParameter("fname", OracleDbType.Varchar2, 20)).Value = participant.FirstName;
        oracleCommand.Parameters.Add(new OracleParameter("vmiddlename", OracleDbType.Varchar2, 12)).Value = participant.MiddleName;
        oracleCommand.Parameters.Add(new OracleParameter("lname", OracleDbType.Varchar2, 35)).Value = participant.LastName;
        oracleCommand.Parameters.Add(new OracleParameter("pname", OracleDbType.Varchar2, 20)).Value = participant.PreferredName;
        oracleCommand.Parameters.Add(new OracleParameter("dob", OracleDbType.Date)).Value = participant.DateOfBirth;
        oracleCommand.Parameters.Add(new OracleParameter("gender", OracleDbType.Varchar2, 4)).Value = participant.Gender;
        oracleCommand.Parameters.Add(new OracleParameter("createdby", OracleDbType.Varchar2, 25)).Value = participant.CreatedBy;
        oracleCommand.Parameters.Add("resultCursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;

        oracleConnection.Open();
        oracleCommand.ExecuteNonQuery();
    }
    catch
    {
        return newParticipant = "";
    }
    oracleConnection.Close();
    return newParticipant;
}

控制器:

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

[HttpPost]
public ActionResult AddNewParticipant(Participant participant)
{
    participant.DateOfBirth = Convert.ToDateTime(participant.DateOfBirth);

    if (ModelState.IsValid)
    {
        string result = DAL.Insert(participant);

        ViewBag.Message = "Participant Added Successfully";
        TempData["addResult"] = result;
        ModelState.Clear();
        return RedirectToAction("ShowAllDetails");
    }            
    else
    {
         ModelState.AddModelError("", "Error in saving data");
         return View();
    }
}

看法:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script src="~/js/DatePicker.js"></script>
</head>
<body>
    <h2>Add New</h2>
    @using (Html.BeginForm())
    {
        <div class="row">
            <div class="col-sm-6 col-xs-12">
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <form asp-action="AddParticipant" method="post" role="form">
                    @Html.AntiForgeryToken()
                    <div class="row form-group">
                        <div class="col-sm-3 addLabel">
                            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class="control-label"})
                        </div>
                        <div class="col-sm-8 addInput">
                            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", @value = "First Name" } })
                            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-sm-3 addLabel">
                            @Html.LabelFor(model => model.MiddleName, htmlAttributes: new { @class = "control-label" })
                        </div>
                        <div class="col-sm-8 addInput">
                            @Html.EditorFor(model => model.MiddleName, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.MiddleName, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-sm-3 addLabel">
                            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label" })
                        </div>
                        <div class="col-sm-8 addInput">
                            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-sm-3 addLabel">
                            @Html.LabelFor(model => model.PreferredName, htmlAttributes: new { @class = "control-label" })
                        </div>
                        <div class="col-sm-8 addInput">
                            @Html.EditorFor(model => model.PreferredName, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.PreferredName, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-sm-3 addLabel">
                            @Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { @class = "control-label" })
                        </div>
                        <div class="col-sm-8 addInput">
                            @Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control date-picker", @placeholder = "mm/dd/yyyy" } })
                            @Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-sm-3 addLabel">
                            @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label" })
                        </div>
                        <div class="col-sm-8 addInput">
                            @Html.DropDownListFor(model => model.Gender, new List<SelectListItem>
                            {
                                new SelectListItem{Text = "Male", Value = "M"},
                                new SelectListItem{Text = "Female", Value = "F"},
                                new SelectListItem{Text = "Transgender", Value = "T"},
                            }, "Choose Gender", new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="addButtons form-group">
                        <input type="submit" value="Submit" class="btn btn-default btn-dark" />
                        <input type="reset" value="Cancel" class="btn btn-default btn-dark" />
                    </div>
                </form>
            </div>
            <h3>@ViewBag.Message</h3>
        </div>
    }
    <div style="color:blue; font-family:Arial">
        @Html.ActionLink("Back to Records", "ShowAllDetails")
    </div>
</body>
</html>

标签: asp.net-mvcoraclestored-proceduresoracle11g

解决方案


推荐阅读