简体   繁体   English

如何将数组从 Javascript 传递给控制器​​的动作?

[英]How to pass array from Javascript to controller's action?

I have JS function like this inside the @section Scripts of .cshtml view:我在.cshtml视图的@section Scripts中有这样的 JS 函数:

function SendIndexes() {
                    var selectedSortOption = $("#SortBy").find('option:selected');
                    var sortIndex = selectedSortOption.val();

                    var selectedFilterByTypeOption = $("#filter_marker_bytype").find('option:selected');
                    var filterByTypeIndex = selectedFilterByTypeOption.val();

                    var selectedFilterByScoreOption = $("#filter_marker_byscore").find('option:selected');
                    var filterByScoreIndex = selectedFilterByScoreOption.val();
                    

                    var selectedFilterByStatusOption = $("#filter_marker_bystatus").find('option:selected');
                    var filterByStatusIndex = selectedFilterByStatusOption.val();

                    var markerFilterIndexes = [filterByTypeIndex, filterByScoreIndex, filterByStatusIndex];


                    location.href = `/Markers/MarkersList?page=@Model.PagingInfo.CurrentPage&sortIndex=${sortIndex}&markerFilterIndexes=${markerFilterIndexes}`
                }

and the controller action signature like this:和控制器动作签名是这样的:

public async Task<IActionResult> MarkersList(List<string> markersForUpdateIds, IEnumerable<int> markerFilterIndexes, int page = 1, string message = "", int sortIndex = 0)
{
...
}

The problem is that the array of three elements is not passed at all.问题是根本没有传递三个元素的数组。 However, if I only push a single variable (element) to this array, then it is passed to the controller's action as it should.但是,如果我只将一个变量(元素)推送到这个数组,那么它会按原样传递给控制器​​的操作。

Why is this happening and how to have all three elements passed to the controller?为什么会发生这种情况以及如何将所有三个元素都传递给控制器​​?

I'll suggest you to use [FromQuery] attribute in your parameter and format your URL like this: myparam=myvalue1&myparam=value2&myparam=myvalue3 .我建议您在参数中使用[FromQuery]属性,并像这样设置 URL 的格式: myparam=myvalue1&myparam=value2&myparam=myvalue3

public async Task<IActionResult> MarkersList([FromQuery]List<string> markersForUpdateIds, [FromQuery]IEnumerable<int> markerFilterIndexes, [FromQuery]int page = 1,[FromQuery] [FromQuery]string message = "", [FromQuery]int sortIndex = 0)
{
...
}

Take a look of ModelBinding: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-3.1#sources看看模型绑定: https ://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding ? view = aspnetcore-3.1#sources

For passing an array to the controller action.用于将数组传递给控制器​​操作。 Data formats that use subscript numbers (... [0] ... [1] ...) must ensure that they are numbered sequentially starting at zero.使用下标数字 (... [0] ... [1] ...) 的数据格式必须确保它们从零开始按顺序编号。 Here you don't need to define an array, just format your url like this:在这里你不需要定义一个数组,只需像这样格式化你的 url:

location.href = `/Markers/MarkersList?page=@Model.PagingInfo.CurrentPage&sortIndex=${sortIndex}&markerFilterIndexes[0]=${filterByTypeIndex}&markerFilterIndexes[1]=${filterByScoreIndex}&markerFilterIndexes[2]=${filterByStatusIndex}`

Since the data in the array is just int type data, you can omit the subscript numbers.由于数组中的数据只是int类型的数据,所以可以省略下标数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数组从控制器传递给 javascript? - How to pass array from controller to javascript? 如何将数据从控制器操作传递到JavaScript函数 - How to pass data from a controller action to JavaScript function 如何将javascript值传递给控制器​​动作? - How to pass javascript value to controller action? Grails-如何将变量从控制器的操作返回到JavaScript? - Grails - how to return a variable from controller's action to JavaScript? 如何从html onchange动作将数组值传递给javascript函数? - how to pass array values to javascript function from html onchange action? 您可以在不使用表单的情况下将数组从javascript传递到asp.net mvc控制器操作吗? - Can you pass an array from javascript to asp.net mvc controller action without using a form? 如何将复合数组从Javascript传递到WEB API控制器HttpPOST? - How to pass a composite array from Javascript to WEB API controller HttpPOST? 将数组从JavaScript传递到viewbag并在控制器中使用 - pass array from JavaScript to viewbag and use it in controller 将JSON数组从控制器传递到JavaScript中的变量 - Pass JSON array from a controller to a variable in JavaScript 将数组从MVC控制器传递到Javascript - Pass Array from MVC Controller to Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM