Home > front end >  Received Model is empty in the controler after post
Received Model is empty in the controler after post

Time:11-07

I have one API in my webservice App that receives an empty model after the post.

This is the code in my testclient that calls the API

private async void AddDriverPayment()
{
    ModelPalmDriverPaymentRequest modelPalmDriverPaymentRequest = new ModelPalmDriverPaymentRequest()
    {
        SCS_ID = int.Parse(gttDXTextEditAddDriverPaymentSCS_ID.Text),
        DriverID = int.Parse(gttDXTextEditAddDriverPaymentDriverID.Text),
        Amount = decimal.Parse(gttDXTextEditAddDriverPaymentAmount.Text),
        Remark = gttDXTextEditAddDriverPaymentRemark.Text,
        PaymentType = gttDXTextEditAddDriverPaymentPaymentType.Text,
        PaymentYear = int.Parse(gttDXTextEditAddDriverPaymentPaymentYear.Text),
        PaymentWeek = int.Parse(gttDXTextEditAddDriverPaymentPaymentWeek.Text),
        DocumentPath = gttDXTextEditAddDriverPaymentDocumentPath.Text,
        DatePayment = dateTimePickerAddDriverPayment.Value
    };

    string JsonData = JsonConvert.SerializeObject(modelPalmDriverPaymentRequest);
    System.Net.Http.StringContent restContent = new StringContent(JsonData, Encoding.UTF8, "application/json");
    HttpClient client = new HttpClient();
    try
    {
        var response = await client.PostAsync(comboBoxEditPalmAddDriverPayment.Text, restContent);

        if (response.IsSuccessStatusCode)
        {
            var stream = await response.Content.ReadAsStringAsync();
            ModelPalmDriverPaymentResponse Result = JsonConvert.DeserializeObject<ModelPalmDriverPaymentResponse>(stream);

            textBoxAddDriverPaymentResult.Text = Result.SCS_ID.ToString()   " "   Result.PaymentID.ToString();
        }
        else
        {
            textBoxAddDriverPaymentResult.Text = response.StatusCode   " "   response.ReasonPhrase;
        }
    }
    catch (Exception ex)
    {
        textBoxAddDriverPaymentResult.Text = ex.Message;
    }

}

And this is the controller code in the webservice

    [Route("palm/AddDriverPayment")]
    [ApiController]
    public class ControllerPalmDriverPayment : ControllerBase
    {
        private readonly RepositoryPalmDriverPayment _repositoryPalmDriverPayment = new();

        [HttpPost]
        public IActionResult AddDriverPayment(ModelPalmDriverPaymentRequest modelPalmDriverPaymentRequest)
        {
            try
            {
                return base.Ok(_repositoryPalmDriverPayment.AddDriverPaymemnt(modelPalmDriverPaymentRequest));
            }
            catch (System.Exception)
            {
                return base.BadRequest("Nope not working...");
            }
        }
    }

The model looks like this (I copied the model class from the service into the client, so I am sure they are exact the same)

    public class ModelPalmDriverPaymentRequest
    {
        public int SCS_ID;
        public int DriverID;
        public decimal Amount;
        public string? Remark;
        public string? PaymentType;
        public int PaymentYear;
        public int PaymentWeek;
        public string? DocumentPath;
        public DateTime DatePayment;
    }

When I try the code, I can see in debug of the testclient that when I post the data, the model is correct filled,

enter image description here

but then I can see in debug on the webservice that the received model is empty

enter image description here

I have other API's in this webservice that I test with the same client, they all do not have this problem.

I found this question but the answers don't help me

Anybody has any idea what the problem here is ?

EDIT

I found the problem, and wrote it in an answer so anybody with the same problem can find it.

CodePudding user response:

Specify Content-Type: application/json on the client side or use the [FromBody] attribute on your ModelPalmDriverPayemntRequest parameter on the controller method.

More details about FromBody attribute here

CodePudding user response:

I have it working now, the problem was I forgot to provide getters and setters in the model

So once I changed this

public class ModelPalmDriverPaymentRequest
{
    public int SCS_ID;
    public int DriverID;
    public decimal Amount;
    public string? Remark;
    public string? PaymentType;
    public int PaymentYear;
    public int PaymentWeek;
    public string? DocumentPath;
    public DateTime DatePayment;
}

into this

public class ModelPalmDriverPaymentRequest
{
    public int SCS_ID { get; set; }
    public int DriverID { get; set; }
    public decimal Amount { get; set; }
    public string Remark { get; set; }
    public string PaymentType { get; set; }
    public int PaymentYear { get; set; }
    public int PaymentWeek { get; set; }
    public string DocumentPath { get; set; }
    public DateTime DatePayment { get; set; }
}

It worked again

  • Related