CodingManiac
CodingManiac

Reputation: 123

Why class object sent to POST is coming NULL?

I have this POST method in api controller.

public void Post(EngineeringData EngineeringData)
{
    EngineeringDataDAL EngDataDAL = new EngineeringDataDAL();

    EngDataDAL.InsertEngineeringData(EngineeringData);
}

I am sending it a test data using POSTMAN.

 EngineeringData = {
    'FunctionalLocation': 'Functional1',
    'WINFileNo': 'WinFileNo-01',
    'EqptType': 'EqptType-01',
    'ComponentTagNo': 'Componenttag-01'
}

but the method's parameter is null. Why?

EngineeringData.cs

public class EngineeringData
{
    public int EnggDataID { get; set; }
    public string FunctionalLocation { get; set; }
    public string WinFileNo { get; set; }
    public string EqptType { get; set; }
    public string ComponentTagNo { get; set; }
}

Upvotes: 1

Views: 51

Answers (3)

Mitra Ghorpade
Mitra Ghorpade

Reputation: 717

{ 
    "FunctionalLocation": "Functional1",
    "WINFileNo": "WinFileNo-01",
    "EqptType": "EqptType-01",
    "ComponentTagNo": "Componenttag-01"
}

Try passing in this data. I believe it has to be valid JSON data to be passed to the controller to accept the parameters. Also not sure if the left parameter before the = sign is needed.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273244

I have this POST method in api controller.

 public void Post(EngineeringData EngineeringData)

What you are probably missing is

 public void Post([FromBody] EngineeringData EngineeringData)

Upvotes: 3

Arshia001
Arshia001

Reputation: 1872

If you're including EngineeringData= in your request, that could be the reason. To the best of my knowledge, ASP.Net expects the body to be plain JSON, not form data.

Upvotes: 2

Related Questions