参数字典在呈现局部视图时包含参数错误的空条目

Los*_*ost 1 ajax asp.net-mvc http razor asp.net-mvc-4

我基本上是想刷新我对一些 clickEvent() 的部分视图,我正在为此做一个 Ajax POST。我的视图如下所示:

<script type="text/javascript">
        $("#employeeGrid").click(function() {
            var grid = $("#employeeGrid").data("kendoGrid");
            var currentSelection = grid.dataItem(grid.select());
            alert(currentSelection.Id);
            $.ajax({
                data: JSON.stringify({ id: 1 }),
                url: "/Employee/ShowEmployeeDetails",
                type: "POST",
                success: function (result) {
                    // refreshes partial view
                    $('#EmployeeDetails').html(result);
                }
            });
        });
      </script>
Run Code Online (Sandbox Code Playgroud)

我的控制器如下所示:

[HttpPost]
        public ActionResult ShowEmployeeDetails(int id)
        {

            List<EmployeeLOAHistory> employeeLoaHistoryList = new List<EmployeeLOAHistory>
            {
                new EmployeeLOAHistory
                {
                    Id = 1,
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now
                },
                new EmployeeLOAHistory
                {
                    Id = 2,
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now
                }
            };
Run Code Online (Sandbox Code Playgroud)

此外,当我在 Firebug 中跟踪我的请求时,它还显示该 ID 已正确发布,并且看起来如下所示:

{"id":1}
Run Code Online (Sandbox Code Playgroud)

但是,响应流讲述了完全不同的故事,如下所示:

<title>The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ShowEmployeeDetails(Int32)'

                return PartialView("_EmployeeDetails");
            }
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会这样。有什么建议?

小智 5

您不需要对 json 对象进行字符串化。将ajax选项更改为

ajax({
    data: { id: 1 }, // change this
    url: '@Url.Action("ShowEmployeeDetails", "Employee")', // recommended
    .....
Run Code Online (Sandbox Code Playgroud)