我不能在mvc核心中使用ajax post作为Json Formatting从动作中获取对象

时间:2021-10-09 07:42:52
 $(document).on("click", "#StudentDelete", function () {
        var Id = $(this).attr("data-id");
        alert(gelenId);
        $.ajax({
            url: '/SchoolRepo/DeleteStudent/' + Id,
            type: "POST",
            datatype: "Json",
            success: function (response) {
                if (response.Success) {
                    location.reload();
                    alert(response.Message);
                }
                else {
                    alert(response.Message);
                }
            }
        });

    });

public class ResultJson
{
    public bool Success { get; set; }
    public string Message { get; set; }
}

 public IActionResult DeleteStudent(int id)
    {
        var student = db.Students.FirstOrDefault(q => q.Id == id);
        if (student!=null)
        {
            db.Students.Remove(student);
            db.SaveChanges();
            return Json(new  ResultJson { Message = "Student is deleted successfully", Success=true });
        }
        return Json(new ResultJson { Message = "Student is not deleted", Success = false });
    }

when I use these code snappets I can't get 'Response.Message' or 'Response.Success' values. always returned 'undefined'. when I used the same codes in mvc 5 I could get values. what is the reason ? thank you in advance

当我使用这些代码时,我无法获得'Response.Message'或'Response.Success'值。总是返回'undefined'。当我在mvc 5中使用相同的代码时,我可以得到值。是什么原因 ?先谢谢你

1 个解决方案

#1


0  

JSON result in ASP.NET Core MVC is using camel case naming strategy by default while serializing the result. So the result will get returned as shown below.

ASP.NET Core MVC中的JSON结果在序列化结果时默认使用驼峰式命名策略。因此结果将返回,如下所示。

{"success":true,"message":"Student is deleted successfully"}

{“success”:true,“message”:“学生已成功删除”}

You can access the result as response.success and response.message

您可以将结果作为response.success和response.message访问

I hope this help you.

我希望这对你有帮助。

https://github.com/aspnet/Mvc/blob/760c8f38678118734399c58c2dac981ea6e47046/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonSerializerSettingsProvider.cs

https://github.com/aspnet/Mvc/blob/760c8f38678118734399c58c2dac981ea6e47046/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonSerializerSettingsProvider.cs

#1


0  

JSON result in ASP.NET Core MVC is using camel case naming strategy by default while serializing the result. So the result will get returned as shown below.

ASP.NET Core MVC中的JSON结果在序列化结果时默认使用驼峰式命名策略。因此结果将返回,如下所示。

{"success":true,"message":"Student is deleted successfully"}

{“success”:true,“message”:“学生已成功删除”}

You can access the result as response.success and response.message

您可以将结果作为response.success和response.message访问

I hope this help you.

我希望这对你有帮助。

https://github.com/aspnet/Mvc/blob/760c8f38678118734399c58c2dac981ea6e47046/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonSerializerSettingsProvider.cs

https://github.com/aspnet/Mvc/blob/760c8f38678118734399c58c2dac981ea6e47046/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonSerializerSettingsProvider.cs