2011-11-08 69 views
2
var post = { "ID":1 }; 

var step = [1,2,3,4,5]; 

post.step = step; 

console.log(post) 

$.ajax({ 
    url:'rss/test', 
    type:'POST', 
    data:post 
}) 

-如何通過json發佈發佈多個數據?

public JsonResult Test(int ID, IEnumerable<string> step) 
{ 
    return Json(true); 
} 

我想通過JSON將它張貼,我該怎麼辦呢?當我試圖做到這一點時,我發現這不起作用。步驟都沒有工作(或郵寄)

更新:設置它後

var post = { 
"ID":1, 
step:[{'Name':'first'},{'Name':'second'}] 
} 

$.post("/rss/test",post,function(){ 
}); 

public JsonResult Test(int ID, IEnumerable<string> step) 
     { 
      return Json(true); 
     } 

我覺得這是工作,但我在即時窗口中有導致

step 
Count = 2 
    [0]: null 
    [1]: null 

所以我希望我走在正確的方向,但兩件事都是空的。現在有人可以幫助我讓他們工作。

感謝

回答

1

服務器上的數據類型不匹配的對象是你從客戶端發送。嘗試這個。

public class Post 
{ 
    public int ID { get; set; } 
    public IEnumerable<string> Step { get; set; } 
} 

然後在你的控制器

[HttpPost] 
public JsonResult Test(Post post) 
{ 

} 

你必須雖然檢查,並沒有對其進行編譯。

要發佈,你必須正確地設置具體的數據類型的數據:

$.ajax({ 
    url:'rss/test', 
    type:'POST', 
    dataType: "json", 
    contentType: "application/json", 
    data:post 
}) 
+0

傳遞作爲JS對象是綁定到行動的參數性能。你的方法也會起作用:) –

0

嘗試添加traditional參數

$.ajax({ 
    url:'rss/test', 
    type:'POST', 
    data:post, 
    traditional: true 
})