1

我有多个参数必须传递给 api 控制器。

我正在做的是

在我的 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");
                    }
                });

& 在我的控制器中

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

我在控制器中得到的是

[["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"]];["

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

4

2 回答 2

2

恐怕你的网址太长(>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");
                        }
                    }
});

和你的控制器:

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

我看到您正在为 routeInfo 使用二维数组。但是只有 1 项,我认为您应该将其更改为一维数组以使其与控制器代码兼容string[] routeInfo

于 2013-04-27T12:43:08.530 回答
2

在这里进入 URL 的信息太多,不仅是您没有正确地将参数附加在一起,还需要使用&not将它们分开;

最重要的是,您并没有真正利用这里的 MVC 功能。在客户端,您希望将信息作为集体对象而不是单个参数发送,例如

var schoolRoutes = {
    routes: routes,
    colors: colorsArray,
    times: mytimeArray,
    distances: myDistanceArray,
    direction: $("#Direction").val()
};

$.ajax({
    type: 'POST'
    url: "api/HomeToSchoolRoutes",
    data: JSON.stringify(schoolRoutes),
    dataType: "json",
    success: function (data) {
        if (data = true) {
            alert("Routes Saved Successfully");
        }
        else if (data = false) {
            alert("Routes are not saved");
        }
});

然后在服务器端,引入一个能够绑定到传入数据的类,也就是ViewModel

public class RouteInfoViewModel
{
    ...
}

public class SchoolRoutesViewModel
{
    public RouteInfoViewModel[] Routes { get; set; }
    public string[] Colours { get; set; }
    public double[] Times { get; set; }
    public double[] Distances { get; set; }
    public string Direction { get; set; }
}

然后更新您的操作以期待这个特定的ViewModel,这应该使您可以访问所有发布的信息。

public bool PostHomeToSchoolRoutes(SchoolRoutesViewModel schoolRoutes)
{
    ...
}
于 2013-04-27T12:50:01.843 回答