2016-07-15 84 views
-1

我的模型:Ajax調用發送空值到控制器

public class Hello 
{ 
    public List<string> name; 
    public List<string> phone; 
    public List<string> contact; 

} 

我的控制器代碼

public ActionResult Home(Hello obj) // obj is coming out to be null 
    { 

    } 

我的劇本是

 var names =[]; 
    var phones =[]; 
    var contacts = []; 

    // some code to fill the arrays 

    var obj = [{ 
      name: names, 
      phone: phones, 
      contact: contacts, 
      }]; 
     debugger; 
     $.ajax({ 
      cache: false, 
      url: 'Home', 
      data: { obj:obj }, 
      success: function (data) { 
       var response = JSON.parse(data); 
       window.location = 'Download?fileGuid=' + response.FileGuid 
            + '&filename=' + response.FileName; 
      } 
     }) 

我可以在數據調試器看到存儲在數組中,但是當我將數據發送到控制器時,obj爲空可以有人建議我在哪裏出錯?

回答

1

你有代碼背後的對象,所以不要傳遞Hello對象的數組。

並且還使用POST請求,因爲GET請求沒有消息體。

請使用以下

var obj = { 
      name: ['1', '2', '3'], 
      phone: ['234324', '34343243', '3434234234'], 
      contact: ['Test1', 'Test2', 'Test3'] 
     }; 
     debugger; 
     $.ajax({ 
      type:'POST', 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      url: 'Home', 
      data:JSON.stringify({ obj: obj }), 
      success: function (data) { 
       var response = JSON.parse(data); 
       // window.location = 'Download?fileGuid=' + response.FileGuid 
            + '&filename=' + response.FileName; 
      } 
     }) 


public class Hello 
{ 
    public List<string> name { get; set; } 
    public List<string> phone { get; set; } 
    public List<string> contact { get; set; } 

} 
+0

對象的值,直到來了爲空 –

+0

你可以在這裏粘貼你的代碼,這樣我可以給你更好的解決方案 –

-1

其實我解決了這個問題,我必須設置

傳統:真正的,

在我的Ajax調用