通过邮递员发送时,后控制器值始终为null

时间:2019-03-15 11:46:18

标签: c# asp.net-mvc

我有MVC体系结构项目。现在我想将api提供给android开发人员。那么如何通过POSTMAN测试POST API服务。当我通过postman发送邮政服务时,它将返回空值。.

url:-http://localhost/Master/AddEmployee

JSON DATA:-
{
    "EmpFName":"Sa",
    "EmpMName":"s",
    "EmpLName":"SS",
     "AadhaarCardNumber":null,
     "PanCardNumber":null,
     "DateOfBirth":"1993-10-20T00:00:00",
     "Address":"Pune",
     "City":"Pune",
     "State":"Maharashtra",
     "Country":"India",
     "PinCode":"410505",
     "EmailID":"sa@GMAIL.COM",
     "Password":"123456",
     "PhotoPath":null,
     "MobileNo":"74487855545",
     "EmergancyContactNo":"7447774558",
     "MaritalStatus":"single",
     "Designation":"devlop",
     "ReportingMgr":1,
     "LeaveCount":18,
     "SpouseName":null,
     "SpDOB":null,
     "SpAge":null,
     "FirstChildName":null,
     "FCDOB":null,
     "FCAge":null,
     "SecondChidName":null,
     "SCDOB":null,
     "SCAge":null,
     "IsActive":true,
     "CreatedBy":null,
     "UpdatedOn":null,
     "Gender":"Female",
     "SCGender":"Male",
     "SpGender":"Female",
     "FCGender":"female"
}

代码:-

[HttpPost]
[Route("addemmployee")]
public ActionResult AddEmployee( FormCollection form)
{
    //employeedata empdata;

    try
    {
        Employee emp = new Employee();

        emp.JoiningDate = Convert.ToDateTime(form["joining_date"]);
        emp.EmpFName = form["txt_fname"];
        emp.EmpMName = form["txt_mname"];
        emp.EmpLName = form["txt_lname"];
        emp.AadhaarCardNumber = form["aadhaarnum"];

        MasterReposities.SaveEmployee(emp);

1 个答案:

答案 0 :(得分:1)

除非您编写自定义绑定程序,否则我认为JSON默认不会序列化为FormCollection。

尝试传递实际的表单:

application/x-www-form-urlencoded 

multipart/form-data

或者要处理JSON,请确保您为项目配置了JSON模型绑定程序(默认情况下应设置Web API),并将model参数更改为实际域对象的方法,然后JSON模型绑定程序将假设您的C#模型属性与JSON属性名称匹配,那么为您工作:

[HttpPost]
[Route("addemmployee")]
public ActionResult AddEmployee(Employee emp)
{
}

如果将其转换为Web API项目,请确保从 ApiController

继承