Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have multiple parameters which I have to pass to api controller.

What I was doing is

In my 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. e.g.

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.

share|improve this question
HomeToSchool is controller name & PostHomeToSchoolRoutes is method in that controller. – vaibhav shah Apr 27 at 13:04
Did not notice that we should not use JSON.stringify in this case but let jquery serialize the parameters for us. There are 3 things to check: do not use JSON.stringify, add "traditional:true", and ensure that the "routes" javascript array is 1-dimensional. Check out my updated answer below (tested) – Khanh TO Apr 28 at 1:51

2 Answers

I'm afraid that your url is too long (>255 characters), You can try this.

$.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. 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

share|improve this answer
its not working for me – vaibhav shah Apr 27 at 12:49
I just updated my answer regarding your 2-dimentional array routeInfo – Khanh TO Apr 27 at 12:50
try updating the datatypes of times, distances, dir to your correct datatypes. See my updated answer. – Khanh TO Apr 27 at 13:10

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 ;.

On top of that, your not really taking advantage of the MVC capabilities here. At the client side you want to send up your information as a collective object rather than individual parameters e.g.

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

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.

public bool PostHomeToSchoolRoutes(SchoolRoutesViewModel schoolRoutes)
{
    ...
}
share|improve this answer
Then at the server side, introduce a class which will be able to bind to the incoming data aka a ViewModel , Didnt understood this line . – vaibhav shah Apr 27 at 20:15
With MVC parameters can be automatically mapped to a class as long as it contains the relevant properties. So define a class and give it properties which you expect to receive from the client & make that the parameter of the action. I have given an example – James Apr 27 at 23:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.