提问人:Shimon Cohen 提问时间:9/9/2021 最后编辑:Shimon Cohen 更新时间:9/9/2021 访问量:50
尝试将数组传递给 ajax 加载函数,但我的控制器没有正确获取数组
Trying to pass an array to ajax load function but my controller doesn't get the array properly
问:
这就是 JQuery:
$(function () {
$('#dish-filter-form').on("submit", function (e) {
e.preventDefault();
var dishName = encodeURIComponent($('#dishName').val());
var selectFoodType = encodeURIComponent($('#selectFoodType').val());
var cities = ["New-York", "Tokyo", "Madrid"];
var strCities= JSON.stringify(cities);
console.log(strCities);
$('.toLoad').load('/Dishes/Search?dishName=' + dishName + "&cities[]=" + strCities+ + "&selectFoodType=" + selectFoodType);
});
});
这是我在 DishesController 中的 Search 方法的签名:
public async Task<IActionResult> Search(string dishName, string[] cities, string selectFoodType)
调试时,我看到 dishName 和 selectFoodType 已被很好地读取,但 cities 始终是一个空数组,尽管我的控制台向我显示数组:如我所愿。strCities
答:
0赞
Twisty
9/9/2021
#1
请考虑以下几点。
$(function () {
$('#dish-filter-form').on("submit", function (e) {
e.preventDefault();
var dishName = encodeURIComponent($('#dishName').val());
var selectFoodType = encodeURIComponent($('#selectFoodType').val());
var cities = ["New-York", "Tokyo", "Madrid"];
var strCities= JSON.stringify(cities);
console.log(strCities);
$('.toLoad').load('/Dishes/Search?dishName=' + dishName + "&cities=" + strCities + "&selectFoodType=" + selectFoodType);
});
});
由于将 Array 转换为 String,因此应将 String 传递到 GET 请求中。
评论
strCities
...cities[]=" + cities + "&...