0

我在 MVC3 中有一个模态对话框,我将参数传递给控制器​​,如下所示:

$("#SelectedCSIDivisionCodes").live("click", function () {
        var ID = $(this).val();

        $.ajax({
            type: "GET",
            url: '@Url.Action("GetCSICodes", "CSICodes")',
            data: { divisionID: ID },
            dataType: "json",
            error: function (xhr, status, error) {
                alert(xhr);
                alert(status);
                alert(error);
                // you may need to handle me if the json is invalid
                // this is the ajax object
            },
            success: function (json) {
                alert("Data Returned: " + json);
            }
        });

        //            $.getJSON(, { divisionID: ID }, function (data) {
        //                alert(data);
        //            });

    });

我收到这样的参数:

   [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult GetCSICodes(string divisionID)
    {
        int ID = Convert.ToInt32(divisionID);
        var csiCodes = (from c in EFProject.CSICode
                        where c.DivisionID == ID
                        select c).OrderBy(x => x.CSICodeID);

        List<SelectListItem> codes = new List<SelectListItem>();

        foreach (var code in csiCodes)
        {
            codes.Add(new SelectListItem { Value = code.CSICodeID.ToString(), Text = code.Code + " " + code.Name });

        }

        return Json(codes.AsEnumerable(), JsonRequestBehavior.AllowGet);
    }

ajax 调用工作正常,但它没有检测到控制器中的参数值。换句话说,我只在控制器中的变量 divisionID 中得到 null。我使用 firefox 查看我是否设置了一个值,并且它似乎正在这样做,因为它返回此 URL:GET http://localhost:1925/CSICodes/GetCSICodes?divisionID%5B%5D=2 [HTTP/1.1 200 OK 95690毫秒]

有什么想法可能是错的吗?

4

1 回答 1

0

Your Action is expecting a parameter of type string, but its getting a parameter of an array type.

Url should end divisionID=2, instead it's ending divisionID[]=2 (an array with 1 value of 2).

In certain circumstances, it's possible for the val() function to return an array. See http://api.jquery.com/val/ for details.

于 2012-04-23T04:11:26.807 回答