1

Hi I'm new to JavaScript and MVC, and I'm trying to pass List and string to my controller.

JavaScript looks like:

 var parms = {
                  globalList: globalList,
                  fieldName: fieldName
              };
              $.ajax({
                  //contentType: 'application/json; charset=utf-8',
                  type: "POST",
                  traditional: true,
                  url: "/Home/SaveField",
                  async: false,
                  data: parms,
                  dataType: "json",
                  success: function (data) {
                      console.log("uspjeh");
                  },
                  error: function (errorData) {
                      console.log("errors");
                  }

              });
          });

and controller looks like:

public void SaveField(List<Point> globalList, String fieldName)
{
  // filedName is correctly passed
  // list is null
}

can somebody help me please?

Point class:

public class Point
{
  [Key]
  public int PointId { get; set; }
  public float GeoDuzina { get; set; }
  public float GeoSirina { get; set; }
  [ForeignKey("Data")]
  public int DataId { get; set; }
  public virtual Data Data { get; set; }
}
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
user3906678
  • 117
  • 1
  • 7
  • 1
    where do you get globalList in your JS code ?? – Selman Genç Aug 04 '14 at 13:02
  • globalList is a list of coordinates from mapbox, they are correct I checkt it {"globalList": [{"GeoDuzina":51.506760996586294,"GeoSirina":-0.06106463202740998},{"GeoDuzina":51.516269286402846,"GeoSirina":-0.056258113472722464},{"GeoDuzina":51.50419662363912,"GeoSirina":-0.044413478462956846}]} – user3906678 Aug 04 '14 at 13:40
  • Show the `Point` class – Zabavsky Aug 04 '14 at 13:49
  • What object did you just post in the comments? If that is supposed to be the `globalList` variable in your javascript code, then you have one nesting too much, it would look like this: `{globalList: { globalList: [..content..] } }` – Dennis Aug 04 '14 at 14:10
  • @user3906678....just change float to double datatype of properties... – Kartikeya Khosla Aug 04 '14 at 14:17
  • I tried changing it to double.. didn't work! And yes it is my globalList, how should I change it to work? – user3906678 Aug 04 '14 at 14:57

1 Answers1

0

It wont work. If you debug a little then you will see that you post to server something like this:

globalList:[object Object]
globalList:[object Object]
globalList:[object Object]

It is just array of strings. But there is a way to do what you want. You can serialize your array to json and then deserialize it in your controller. Just change your param to:

var parms = {
    globalList: JSON.stringify(globalList),
    fieldName: fieldName
};

And action:

[HttpPost]
public void SaveField(string globalList, string fieldName)
{
    var serializer = new JavaScriptSerializer(); //use any serializer you want
    var list = serializer.Deserialize<List<Point>>(globalList);
}

Or you can make param object looks like from HTML form:

var parms = {
     "globalList[0].GeoDuzina": 51.506760996586294, 
     "globalList[0].GeoSirina": -0.06106463202740998,
     "globalList[1].GeoDuzina": 51.516269286402846, 
     "globalList[1].GeoSirina": -0.056258113472722464,
     "globalList[2].GeoDuzina": 51.50419662363912, 
     "globalList[2].GeoSirina": -0.044413478462956846,
     fieldName: fieldName
 };

It works with your action.

There are many other ways to do that. I just show two of them. Is any good for you?

btw. This numbers are too long for floats.;)

py3r3str
  • 1,879
  • 18
  • 23