[英]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.