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

时间:2015-03-09 23:44:21

标签: ajax asp.net-mvc asp.net-mvc-4 http razor

我基本上是尝试刷新一些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>

我的控制器如下:

[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
                }
            };

另外,当我在Firebug中跟踪我的请求时,它还会显示id已正确发布并且看起来像下面的内容:

{"id":1}

然而,响应流告诉完全不同的故事,它看起来如下:

<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");
            }

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

1 个答案:

答案 0 :(得分:4)

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

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