如何将这个js数组传递给我的MVC 3控制器?

问题描述:

我得到控制器中的空值。不知道我错过了什么。如何将这个js数组传递给我的MVC 3控制器?

我有一个网格,我有一个访客列表(名称为&电子邮件),其中用户通过复选框选择访客。

然后我读取所选联系人的名称和电子邮件并构建js数组。 然后这个数组传递给MVC 3 controller

JS代码:

var name ='', email=''; 
    var guest = new Array(); 
      var guests = new Array(); 
      $('.CBC').each(function() { //loop grid by checkbox class 
       if (this.checked) { 
        name = GetSelectedName(); 
        email = GetSelectedEmail(); 
        guest = { 'Email': email, 'Name': name }; 
        guests.push(guest); 
       } 
      }); 

     $.ajax({ 
     type: "POST", 
     url: GetURL(), 
     data: guests, 
     dataType: "json", 
     success: function (res) { 
      //do something 
     } 
}); 

控制器:

[HttpPost] 
    public ActionResult AddGuests(List<SelectedGuest> guests) 
    {    
     GuestService svc = new GuestService(); 
     //do something with guests 
     //But Name and Email of all items in guests are null!!! 
    } 

public class SelectedGuest 
{ 
    //represent the email columns of the contact grid 
    public string Email { get; set; } 

    //represent the Name column of the contact grid 
    public string Name { get; set; } 
} 

我需要JS数组显式转换为JSON对象序列化呢?

+1

你怎么在'串id'指定? – 2011-06-13 05:48:20

+0

Id是一个字符串。它得到正确传递。为了清晰起见,我删除了Id参数。 – kheya 2011-06-13 06:08:32

如果你有使用插件的*,尝试jQuery-JSON

var guests = new Array(); 

// push stuff into the array 

$.ajax({ 
    type: "POST", 
    url: GetURL(), 
    data: $.toJSON(guests), 
    dataType: "json", 
    success: function (res) { 
     //do something 
    } 
); 
+0

这是最好的选择吗? – kheya 2011-06-13 06:57:43

+0

这就是我使用的。这很简单直接。我可以考虑修改html的另一个选项,然后使用'serializeArray()'发布到控制器。如果需要,我会详细说明。 – 2011-06-13 07:23:47

+1

为什么jQuery插件json -.-只是使用'JSON.stringify' – Raynos 2011-06-13 07:35:16

也许改变设置传统到真正可能的帮助。这里是我修改过的代码,用于将唯一标识符(Guid)发布到控制器操作。

var yourArray = new Array(); 
// TODO: fill array with ids of checked checkboxes 
$('.CBC:checked').each(function() { 
    yourArray.push($(this).attr('myId')); 
}); 

var postData = { 
    yourArray: yourArray 
}; 

$.ajax({ 
    type: "POST", 
    url: "/ctrl/ActionName", 
    data: postData, 
    success: function (result) { 
    }, 
    datatype: "json", 
    traditional: true 
}); 

在控制器中,我有以下操作。

[HttpPost] 
public ActionResult ActionName(List<Guid> yourArray) 
{ 
    return View(); 
} 
+0

我的ajax调度器是通用的。我不喜欢传统:除非这是我最后的选择,否则只适用于这个用例。你的例子比较简单,因为它只有ID。我有另一个数组内的数组列表。 [{name:“”,email:“”},{},{}] – kheya 2011-06-13 07:06:42

您还可以创建自定义模型联编程序。这使您可以将原始输入中的代码写入请求对象,并从中创建一个对象。这可以让你创建一个字符串列表或其他你想在控制器中看到的对象。它也是非常可重用的。

$.ajax({ 
      type: "POST", 
      url: "yourUrl", 
      data: JSON.stringify(yourArray), 
      contentType: "application/json; charset=utf-8" 
     }); 

的contentType: “应用/ JSON的;字符集= UTF-8” - 是很重要的一部分

[HttpPost] 
public void Fake(List<yourType> yourArray) 
{ 
} 
+0

这不适合我。当我设置contenType时,根本没有数据通过! – M46 2018-02-07 14:14:45

,因为你在发送错误的方式JSON数组你得到空。

首先,你应该序列的JSON数组:

$.ajax({ 
     // Whatever ... 
     type: "POST", 
     url: "yourUrl", 
     data: JSON.stringify(guests), 
     // Whatever ... 
    }); 

其次,你的控制器应得到的字符串:

[HttpPost] 
public ActionResult ActionName(string guests) 
{ 
    // ... 
} 

最后,你应该反序列化字符串相关类型:

[HttpPost] 
public ActionResult ActionName(string guests) 
{ 
    // this line eserializes guests ... 

    IList<GuestType> gs = 
     new JavaScriptSerializer().Deserialize<IList<GuestType>>(guests); 

    // ... Do whatever with gs ... 

    return View(); 
} 

我试过这个,它的工作。

var name ='', email=''; 
var guest = new Array(); 
     var guests = new Array(); 
     $('.CBC').each(function() { //loop grid by checkbox class 
      if (this.checked) { 
       name = GetSelectedName(); 
       email = GetSelectedEmail(); 
       guest = { 'Email': email, 'Name': name }; 
       guests.push(guest); 
      } 
     }); 

var PostData = { guests: guests }; //if this is creating ambiguity try using something:guest //(something is //controller parameter, change your controller parameter accordingly)    

    $.ajax({ 
    type: "POST", 
    url: GetURL(), 
    data: PostData , 
    dataType: "json", 
    success: function (res) { 
     //do something 
    } 
}); 

干杯,

SRINIVAS

刚刚设置的“传统:真正的”在阿贾克斯。

$.ajax({ 
    type: "POST", 
    url: GetURL(), 
    data: PostData , 
    dataType: "json", 
    traditional:true, 
    success: function (res) { 
     //do something 
    } 
});