2016-06-28 72 views
1

創建ajax調用時,僅獲取第二個參數。無法獲取MVC控制器中的所有ajax參數

阿賈克斯:

控制器: -

public string AddEmployee(EmpDetail Emp, int? File_ID) 
     { 
     .... 
} 

型號: -

public partial class EmpDetail 
    { 
     public int Id { get; set; } 
     public string name { get; set; } 
     public System.DateTime DOB { get; set; } 
     public string Gender { get; set; } 
     public string Email { get; set; } 
     public string Mobile { get; set; } 
     public string Address { get; set; } 
     public System.DateTime JoiningDate { get; set; } 
     public int DepartmentID { get; set; } 
     public int DesignationID { get; set; } 
     public Nullable<int> FileId { get; set; } 
     public Nullable<int> CountryId { get; set; } 
     public Nullable<int> StateId { get; set; } 
    } 

我也曾嘗試

data: JSON.stringify(employee,File_ID) 

我知道這是錯誤的,但在這裏我只得到第一個參數。

+0

您是否嘗試過直接供應的對象?將JSON字符串一起打包是一種非常脆弱的方法,並且將'EmpDetail'作爲字符串提供給ModelBinder可能會產生問題。 'data:{Emp_Detail:employee,File_ID:File_ID},' –

+0

我已經試過這個以及這個數據:{EmpDetail:JSON.stringify(employee),File_ID:File_ID},但只獲得第二個參數1st blank是 –

回答

2

試試這個,

var empData= JSON.stringify({ EmpDetail: employee, File_ID: File_ID }); 

和AJAX,

return $http({ 
      method: "post", 
      url: "/myform/AddEmployee", 
      data: empData, 
      dataType: "json" 
     }) 

希望幫助。

+0

EmpDetails在這裏是空白的,只有獲得file_ID –

+0

@SunilChaudhry你的員工變量應該與你在控制器中所期望的參數具有相同的名稱。 – Berkay

+0

我通過相同的名字 –

0

您可以發送JavaScript對象而不是字符串

return $http({ 
       method: "post", 
       url: "/myform/AddEmployee", 
       data: { EmpDetail: employee, File_ID: File_ID },    
       dataType: "json" 
      }) 
+0

EmpDetails是空白的,只能得到file_ID –

+0

你能更新你的代碼並把員工變量的值 – Kld

+0

EmpDetails或EmpDetail?你發送的變量是EmpDetail – Kld

相關問題