Ajax调用未在控制器中输入方法

时间:2019-09-05 12:11:15

标签: ajax asp.net-mvc entity-framework asp.net-ajax c#-3.0

Ajax调用未到达控制器中的方法。我写了ajax调用来更新db中的记录。但这使我误认为是错误的结果。怎么了?

EmployeeModel.cs

public static async Task<bool> AddOrUpdSubClassificationLevel(EmpEvaluation_SubLevels obj)
{
    try
    {
        using (var entities = new WebPortalEntities())
        {
            entities.EmpEvaluation_SubLevels.AddOrUpdate(obj);
            await entities.SaveChangesAsync();
        }
        return true;
    }
    catch (Exception ex)
    {
        CommonHelper.WriteError($"AddSubClassificationLevel ERROR: {JsonConvert.SerializeObject(ex)}");
    }
    return false;
}

EmployeeController.cs

[HttpPost]
        public async Task<JsonResult> CreateSubClassification(EmpEvaluation_SubLevels obj)
        {
            obj.AuthorId = UserID;
            obj.CreatedDate = DateTime.Now;
            var result = await EmployeeModel.AddOrUpdSubClassificationLevel(obj);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

Ajax本身

$('#submit-edited-sublevel').on('click',
            function() {
                if ($('#idOfSubPost').val()) {
                    showGlobalLoadingWrapper();
                    var editedclassificationsublevel = {
                        Id: parseInt($('#idOfSubPost').val()),
                        EmpEvaluationLevelsId: $('#idOfLevel').val(),
                        Name: $('#edited-sublevel-name').val(),
                        AuthorId: 1,
                        CreatedDate: new Date()
                    }


                    $.ajax({
                        type: "POST",
                        contentType: 'application/json, charset=utf-8',
                        url: "/Employee/CreateSubClassification",
                        data: JSON.stringify(editedclassificationsublevel),
                        success: function(data) {
                            $("#editSubClassificationModal").modal("hide");
                            hideGlobalLoadingWrapper();
                            getClassificators();
                        },
                        error: function(error) {
                            hideGlobalLoadingWrapper();
                            console.log(error);
                        }
                    });
                }

            });

预期结果为true。我认为它必须来自EmployeeModel.cs。这是用于更新提供的数据的功能。这也应该在db中进行提交。

1 个答案:

答案 0 :(得分:0)

$('#submit-edited-sublevel').on('click',
        function() {
            if ($('#idOfSubPost').val()) {
                showGlobalLoadingWrapper();
                var editedclassificationsublevel = {
                    Id: parseInt($('#idOfSubPost').val()),
                    EmpEvaluationLevelsId: $('#idOfLevel').val(),
                    Name: $('#edited-sublevel-name').val(),
                    AuthorId: 1,
                    CreatedDate: new Date()
                }


                $.ajax({
                    type: "POST",
                    contentType: 'application/json, charset=utf-8',
                    url: "/Employee/CreateSubClassification",
                    data: obj,
                    success: function(data) {
                        $("#editSubClassificationModal").modal("hide");
                        hideGlobalLoadingWrapper();
                        getClassificators();
                    },
                    error: function(error) {
                        hideGlobalLoadingWrapper();
                        console.log(error);
                    }
                });
            }

        });


[HttpPost]
    public async Task<ActionResult> CreateSubClassification(EmpEvaluation_SubLevels obj)
    {
        obj.AuthorId = UserID;
        obj.CreatedDate = DateTime.Now;
        var result = await EmployeeModel.AddOrUpdSubClassificationLevel(obj);
        return Json(result, JsonRequestBehavior.AllowGet);
    }

请尝试上面的示例删除json.stringify。