0

I have web API

I try to send request from postman

Here is my Model

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace trackingappbackend.Models
{
    using System;
    using System.Collections.Generic;

    public partial class StartWorkingDay
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public int Id { get; set; }
        public string Company { get; set; }
        public string Date { get; set; }
        public string Time { get; set; }
        public string INN { get; set; }
    }
}

And here is controller

// POST: api/StartWorkingDays
    [ResponseType(typeof(StartWorkingDay))]
    public IHttpActionResult PostStartWorkingDay(StartWorkingDay startWorkingDay)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.StartWorkingDays.Add(startWorkingDay);
        db.SaveChanges();

        return CreatedAtRoute("DefaultApi", new { id = startWorkingDay.Id }, startWorkingDay);
    }

I pass data like on screen

enter image description here

But I have this error

Value cannot be null. Parameter name: entity

How I can fix it?

Sukhomlin Eugene
  • 183
  • 4
  • 19

1 Answers1

0

You are sending the data in the request body and your Post method doesn't know that. You can change your Post method to add [FromBody] to extract the data from the body.

[ResponseType(typeof(StartWorkingDay))]
public IHttpActionResult PostStartWorkingDay([FromBody]StartWorkingDay startWorkingDay)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    db.StartWorkingDays.Add(startWorkingDay);
    db.SaveChanges();

    return CreatedAtRoute("DefaultApi", new { id = startWorkingDay.Id }, startWorkingDay);
}

also, try to debug your method to see if your startWorkingDay parameter is deserialized with the data sent.

Hope that helpes.

Omar

Omar.Alani
  • 4,050
  • 2
  • 20
  • 31