简体   繁体   English

将列表和字符串数据从Ajax传递到MVC 4中的控制器,列表是否为NULL?

[英]passing List and String data from ajax to controller in mvc 4, getting NULL for list?

Hi I'm new to JavaScript and MVC, and I'm trying to pass List and string to my controller. 嗨,我是JavaScript和MVC的新手,我正在尝试将List和string传递给我的控制器。

JavaScript looks like: JavaScript看起来像:

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

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. 您可以将数组序列化为json,然后在控制器中反序列化。 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: 或者,您可以从HTML表单中使param对象看起来像:

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.;) 这个数字对于浮点数来说太长了。

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

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