为什么当我插入数据库服务器时出现500错误

时间:2020-03-16 07:37:38

标签: .net asp.net-core

我还没有看到这个,我需要帮助。 1.当我在数据库中执行插入操作时,它会传递(发布数据),但是脚本无法正常运行,并且在控制台中,我看到500错误。 该错误某种程度上与PartialView相关

enter image description here

  1. 以及插入的原因,但在控制台中没有到达调试点 enter image description here
  2. 以及为什么渲染PartialView,然后将布局放在页脚上。如何解决? 这是发布方法

            public async Task<IActionResult> Create([FromBody] Employee employee)
            {
            if (ModelState.IsValid)
            {
                _context.Add(employee);
                await _context.SaveChangesAsync();
                return PartialView("EmployeeList", _context.Employees.ToList());
            }
    
            ViewData["PositionId"] = new SelectList(_context.Positions, "PositionId", "PositionId", employee.PositionId);
            return View(employee);
        }
    

这是我的模特

public class Employee
{
    [Key] 
    public int EmployeeId { get; set; }
    [Required] 
    public string FistName { get; set; }
    [Required] 
    public string LastName { get; set; }

    [Required(ErrorMessage = "Enter Position")]
    public int PositionId { get; set; }
    public Position Position { get; set; }
}

这是PartialView

<table class="table">
    <tr>
        <th>Fist Name</th>
        <th>Last Name</th>
        <th>Position</th>
        <th>Salary</th>
        <th>Date Start</th>
        <th>Date End</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.FistName</td>
            <td>@item.LastName</td>
            <td>@item.Position.PositionName</td>
            <td>@item.Salary</td>
            <td>@item.DateStart</td>
            <td>@item.DateEnd</td>

            <td>
                <a asp-action="Edit" asp-route-id="@item.EmployeeId">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.EmployeeId">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.EmployeeId">Delete</a>
            </td>

        </tr>
    }
</table>

这是视图

@{
    ViewData[index: "Title"] = "Create";
}

<h3>Create Employee</h3>
<hr/>
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="addEmployeeLabel">Add Employee</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <form asp-action="Create">
                <input name="IsValid" type="hidden" value="@ViewData.ModelState.IsValid.ToString()"/>
                <div class="form-group">
                    <label asp-for="FistName"></label>
                    <input asp-for="FistName" class="form-control"/>
                    <span asp-validation-for="FistName" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="LastName"></label>
                    <input asp-for="LastName" class="form-control"/>
                    <span asp-validation-for="LastName" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Position" class="control-label"></label>
                    @Html.DropDownList("PositionId", (IEnumerable<SelectListItem>) ViewData["PositionId"], "- Select -", new {id = "PositionId"})
                    <span asp-validation-for="Position" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Salary"></label>
                    <input asp-for="Salary" class="form-control"/>
                    <span asp-validation-for="Salary" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="DateStart"></label>
                    <input asp-for="DateStart" class="form-control"/>
                    <span asp-validation-for="DateStart" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="DateEnd"></label>
                    <input asp-for="DateEnd" class="form-control"/>
                    <span asp-validation-for="DateEnd" class="text-danger"></span>
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary" data-save="modal" id="save-employee">Save</button>
        </div>
    </div>
</div>

最后一个js

$("#addEmployee").click(function() {

    $.ajax({url: $(this).attr("formaction")
    }).done(function(msg) {
        $("#AddEmployee").html(msg);
        $("#add-employee").modal("show");
    });
});
$("#save-employee").off("click").on("click",
    function () {
        //e.preventDefault();
        var form = $('form');
        var token = $('input[name="__RequestVerificationToken"]', form).val();
        var data_ = {
            //__RequestVerificationToken: token,
            FistName: $("#FistName").val(),
            LastName: $("#LastName").val(),
            PositionId: parseInt($("#PositionId").val()),
            Salary: parseFloat($("#Salary").val()),
            DateStart: $("#DateStart").val(),
            DateEnd: $("#DateEnd").val()
        }

        $.ajax({
            type: "post",
            url: form.attr('action'),
            data: JSON.stringify(data_),
            dataType: "html",
            contentType: "application/json; charset=utf-8",
            success: function(result) {
                $("#add-employee").modal("hide");
                $("#partial").html(result);
            }
        });
        return false;
    });

1 个答案:

答案 0 :(得分:0)

可能丢失数据或违反了插入本身的约束。放置一个调试点:

await _context.SaveChangesAsync();

然后按F10键,查看异常消息是什么