将ID从所选项目传递到控制器

问题描述 投票:0回答:1

我的视图上有一个脚本,可将ID从选定的值传递到我的Controller:

<script type="text/javascript">
    $('#Group').change(function () {
        var selectedGroup = $("#Group").val();
        var categoriesSelect = $('#Category');
        categoriesSelect.empty();
        if (selectedGroup != null && selectedGroup != '') {
            $.getJSON('@Url.Action("GetCategories")', { Id: selectedGroup }, function (categories) {
                if (categories != null && !jQuery.isEmptyObject(categories))
                {
                    categoriesSelect.append($('<option/>', {
                        value: null,
                        text: ""
                    }));
                    $.each(categories, function (index, category) {
                        categoriesSelect.append($('<option/>', {
                            value: category.Value,
                            text: category.Text
                        }));
                    });
                 };
            });
        }
    });
</script>

这是我控制器上的功能:

    [HttpGet]
    public ActionResult GetCategories(int groupId)
    {
        //...
    }

选择更改事件有效,但是与Get请求一起发送的参数groupId始终为0。我忘记了什么?

javascript c# model-view-controller asp.net-core-2.2
1个回答
2
投票

{Id:selectedGroup}表示您正在传递名称为Id的参数,该参数在您的方法中不存在。可以将其替换为{groupId:selectedGroup},也可以将方法签名更改为:

    public ActionResult GetCategories(int Id)
© www.soinside.com 2019 - 2024. All rights reserved.