2

I am having a Web Api containing Model -

 public class Assessments
{
    public List<AssessmentResult> Results { get; set; }
}

public class AssessmentResult
{
    public int? AssessorId { get; set; }
    public int? AssessmentId { get; set; }
    public int? CentreId { get; set; }
    public int? BatchId { get; set; }
    public DateTime AssessmentDate { get; set; }

And Controller -

[HttpPost]
    [ActionName("SetAssessmentResults")]
    public HttpResponseMessage SetAssessmentResults([FromBody] Assessments assessments)
    {
        try
        {

            string sproc = ConfigurationManager.AppSettings["ASSESSORAPP_InsertAssessmentsStaging"];
            //_dt = Helper.ToDataTable(assessments); use this if sending list doesnt work

            Mapper.Initialize(cfg =>
            {
                //cfg.CreateMissingTypeMaps = false;
                cfg.CreateMap<AssessmentResult, AssessmentTable>().IgnoreAllNonExisting();

                //cfg.IgnoreUnmapped();
            });
            List<AssessmentTable> assessmentTable = Mapper.Map<List<AssessmentResult>, List<AssessmentTable>>(assessments.Results);
            Mapper.Configuration.AssertConfigurationIsValid();

            string isSuccess = _db.InsertAssessmentResults(sproc, assessmentTable);
            if (isSuccess.Split('$')[0].ToLower() == "success")
            {
                return Request.CreateResponse(HttpStatusCode.OK, isSuccess);
            }
            else if (isSuccess.Split('$')[0].ToLower() == "duplicate")
            {
                return Request.CreateResponse(HttpStatusCode.OK, isSuccess);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.OK, isSuccess);
            }
        }
        catch (Exception ex)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
        }
    }

Client side, I am using Postman Chrome extension. I am sending POST request with JSON data in body. However, I am not getting the JSON data in controller (assessments object always shows NULL value)

assessments object showing NULL value

In client side, I am request using PostMan Chrome extension

PostMan Client sending request

This is my JSON data :

    {
"assessment":
[{
      "AssessorId": 3,
      "AssessmentId": 123,
      "CentreId": 1,
      "BatchId": null,
      "AssessmentDate": "2016-09-30T00:00:00",

    },{
     "AssessorId": 2,
      "AssessmentId": 123,
      "CentreId": 1,
      "BatchId": null,
      "AssessmentDate": "2016-09-30T00:00:00",
    }]
    }

I have also added Content-Type : application/json

Please help me to solve this problem. Thank you !

3
  • 1
    rename "assessment" to "Results" in your json Commented Sep 28, 2016 at 13:17
  • Make sure the name of your properties in JSON are identical to your C# model class. Commented Sep 28, 2016 at 13:19
  • It worked..... Thank you sooooo much :) Commented Sep 29, 2016 at 4:49

1 Answer 1

1

In your class Assessments you name the property that contains the list of AssessmentResult "Results" but when you send the post you are naming the property "assessment" it should be "Results" (considering you have the default serializer configuration eg: no propertie name to lower camel case when serializing)

Try:

  {
     "Results":[{
                 "AssessorId": 3,
                 "AssessmentId": 123,
                 "CentreId": 1,
                 "BatchId": null,
                 "AssessmentDate": "2016-09-30T00:00:00",

               },
               {
                 "AssessorId": 2,
                 "AssessmentId": 123,
                 "CentreId": 1,
                 "BatchId": null,
                 "AssessmentDate": "2016-09-30T00:00:00",
               }]
 }

Also as a good practice you should instance the list in the constructor of your class Eg:

public class Assessments
{
    public Assessments()
    {
         Results = new List<AssessmentResult>();
    }
    public List<AssessmentResult> Results { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.