gpt4 book ai didi

javascript - 如何在mvc4中将多个参数从 View 发送到api Controller

转载 作者:行者123 更新时间:2023-11-30 10:32:19 24 4
gpt4 key购买 nike

我有多个参数必须传递给 api Controller 。

我做的是

在我的 javascript 中

            var routeInfo = JSON.stringify(routes);
var colors = JSON.stringify(colorsArray);
var times = JSON.stringify(mytimeArray);
var distances = JSON.stringify(myDistancArray);
var dir = $("#Direction").val();

var fullString = routeInfo + ";" + colors + ";" + times + ";" + distances+";"+dir;

$.post("api/HomeToSchool/?route=" + fullString,
function (data) {
if (data = true) {
alert("Routes Saved Successfully");
}
else if (data = false) {
alert("Routes are not saved");
}
});

& 在我的 Controller 中

public bool PostHomeToSchoolRoutes([FromUri]string route)
{
// my logic
}

这里我只是获取“routeInfo”的值,其他值没有出现。例如

var routeInfo =  [["Børge,Brogade  38, 4100, Ringsted,09:25:00,55.43953, 11.79043","Grete,Sjællandsgade  27, 4100, Ringsted,09:25:00,55.44024, 11.78852","Else,Fynsgade  14, 4100, Ringsted,09:25:00,55.44128, 11.78595","Birthe,Eksercerpladsen  47, 4100, Ringsted,09:25:00,55.44954, 11.80309","Knud Lavard Centret, Eksercerpladsen 3,  4100,  Ringsted,370,55.45014, 11.80474"]]

var colors = ["#886A52"]
var times = [7.97]
var distances = [3.36]
var dir = 0

我在 Controller 中得到的是

[["Børge,Brogade  38, 4100, Ringsted,09:25:00,55.43953, 11.79043","Grete,Sjællandsgade  27, 4100, Ringsted,09:25:00,55.44024, 11.78852","Else,Fynsgade  14, 4100, Ringsted,09:25:00,55.44128, 11.78595","Birthe,Eksercerpladsen  47, 4100, Ringsted,09:25:00,55.44954, 11.80309","Knud Lavard Centret, Eksercerpladsen 3,  4100,  Ringsted,370,55.45014, 11.80474"]];["

其他值不会出现。我在这里做错了什么。

最佳答案

我担心你的 url 太长(>255 个字符),你可以试试这个。

$.ajax({
type: 'POST',
url:"api/HomeToSchool",
data:{routeInfo:routes,colors:colorsArray,times:mytimeArray,distances:myDistancArray,dir:dir},
dataType: "json",
traditional:true,
function (data) {
if (data = true) {
alert("Routes Saved Successfully");
}
else if (data = false) {
alert("Routes are not saved");
}
}
});

和你的 Controller :

public bool PostHomeToSchoolRoutes(string[] routeInfo,string[] colors,double[] times,double[] distances,int dir)
{
// my logic
}

我看到您正在为 routeInfo 使用二维数组。但是只有一项,我觉得你应该把它改成一维数组来兼容 Controller 代码 string[] routeInfo

关于javascript - 如何在mvc4中将多个参数从 View 发送到api Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16252132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com