简体   繁体   English

如何在mvc4中将多个参数从视图发送到api控制器

[英]how to send multiple parameters from view to api controller in mvc4

I have multiple parameters which I have to pass to api controller. 我有多个参数,我必须传递给api控制器。

What I was doing is 我在做的是

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

& in my Controller 在我的控制器中

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

Here I am just getting values of "routeInfo" & other values are not comming. 在这里,我只是获取“routeInfo”的值,其他值不会出现。 eg 例如

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

What I get in my Controller is 我在控制器中得到的是

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

Other values are not coming. 其他价值观没有到来。 Whats wrong I am doing here. 我在这做什么错了。

I'm afraid that your url is too long (>255 characters), You can try this. 我担心你的网址太长(> 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");
                        }
                    }
});

and your controller: 和你的控制器:

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

I see that you're using 2-dimensional array for routeInfo. 我看到你正在为routeInfo使用二维数组。 But there is only 1 item, i think you should change it to 1-dimensional array to make it compatible with the controller code string[] routeInfo 但是只有1个项目,我认为你应该将它改为1维数组,以使其与控制器代码string[] routeInfo兼容string[] routeInfo

Far too much information going into the URL here, not only that your not correctly appending the parameters together they need to be separated using & not ; 这里有太多的信息进入URL,不仅是你没有正确地将参数附加到一起,而是需要使用& not分开它们; .

On top of that, your not really taking advantage of the MVC capabilities here. 最重要的是,你并没有真正利用MVC功能。 At the client side you want to send up your information as a collective object rather than individual parameters eg 在客户端,您希望将您的信息作为集体对象而不是单个参数发送,例如

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

Then at the server side, introduce a class which will be able to bind to the incoming data aka a ViewModel 然后在服务器端,引入一个类,该类将能够绑定到传入数据,即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; }
}

Then update your action to expect this particular ViewModel and that should give you access to all the information posted. 然后更新您的操作以期望此特定ViewModel ,这应该允许您访问所有发布的信息。

public bool PostHomeToSchoolRoutes(SchoolRoutesViewModel schoolRoutes)
{
    ...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM