Sunil Chaudhry Sunil Chaudhry - 5 months ago 11
jQuery Question

Unable to get all ajax parameters in MVC Controller

When creating ajax call to controller getting 2nd parameter only.

Ajax:-

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


Controller:-

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


Model:-

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


I have also tried

data: JSON.stringify(employee,File_ID)


I know this is wrong, but here I am getting first parameter only.

Answer

Try this,

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

And ajax,

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

Hope helps.