将列表和字符串数据从Ajax传递到MVC 4中的控制器,列表是否为NULL? [英] passing List and String data from ajax to controller in mvc 4, getting NULL for list?

查看:52
本文介绍了将列表和字符串数据从Ajax传递到MVC 4中的控制器,列表是否为NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaScript和MVC的新手,我正在尝试将List和string传递给我的控制器.

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

JavaScript如下:

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

              });
          });

和控制器看起来像:

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

有人可以帮我吗?

积分班:

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]

它只是字符串数组.但是有一种方法可以做您想要的.您可以将数组序列化为json,然后在控制器中反序列化.只需将参数更改为:

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

动作:

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

或者您可以通过HTML形式使param对象看起来像

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

这篇关于将列表和字符串数据从Ajax传递到MVC 4中的控制器,列表是否为NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆