0

I created a webapi by looking at the ms basic documentation.

ms Doc

it was work todoitems

Besides todoitems I Because I wanted to use it in the form of Avatar Chart Insert the model Avatarchart, create Avatarchartcontext, and make the controller as a scaffold.

solution view

However, when I tried to receive data as a post, the data always came in null. error

null

this is code

[Route("api/AvatarCharts")]
    [ApiController]
    public class AvatarChartsController : ControllerBase
    {
        private readonly AvatarChartContext _context;

        public AvatarChartsController(AvatarChartContext context)
        {
            _context = context;
        }

        // GET: api/AvatarCharts
        [HttpGet]
        public async Task<ActionResult<IEnumerable<AvatarChart>>> GetAvatarCharts()
        {
            return await _context.AvatarCharts.ToListAsync();
        }

        // GET: api/AvatarCharts/5
        [HttpGet("{modelId}")]
        public async Task<ActionResult<AvatarChart>> GetAvatarChart(long modelId)
        {
            var avatarChart = await _context.AvatarCharts.FindAsync(modelId);

            if (avatarChart == null)
            {
                return NotFound();
            }

            return avatarChart;
        }

        // PUT: api/AvatarCharts/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{modelId}")]
        public async Task<IActionResult> PutAvatarChart(long modelId, AvatarChart avatarChart)
        {
            if (modelId != avatarChart.modelId)
            {
                return BadRequest();
            }

            _context.Entry(avatarChart).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AvatarChartExists(modelId))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/AvatarCharts
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<AvatarChart>> PostAvatarChart( AvatarChart avatarChart)
        {
            _context.AvatarCharts.Add(avatarChart);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetAvatarChart", new { modelId = avatarChart.modelId }, avatarChart);
        }

        // DELETE: api/AvatarCharts/5
        [HttpDelete("{modelId}")]
        public async Task<IActionResult> DeleteAvatarChart(long modelId)
        {
            var avatarChart = await _context.AvatarCharts.FindAsync(modelId);
            if (avatarChart == null)
            {
                return NotFound();
            }

            _context.AvatarCharts.Remove(avatarChart);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool AvatarChartExists(long modelId)
        {
            return _context.AvatarCharts.Any(e => e.modelId == modelId);
        }
    }
 public class AvatarChartContext :DbContext
    {
        public AvatarChartContext(DbContextOptions<AvatarChartContext> options) : base(options)
        {

        }
        public DbSet<AvatarChart> AvatarCharts { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AvatarChart>().HasKey(c => c.modelId);

            
        }
    }
public class AvatarChart
    {
        [Key]
        public long modelId;
        public string colorCode;
        public long dateTime;        //ex 20210101130000
        public string name;
        public string diagnose;

    }

swagger

add postgressql dbenter image description here

1

2 Answers 2

1

Update:

You missed the {get; set;}.

public class AvatarChart
{
    [Key]
    public long modelId {get; set;}
    public string colorCode {get; set;}
    public long dateTime {get; set;}        //ex 20210101130000
    public string name {get; set;}
    public string diagnose {get; set;}

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for answering i did that it was not work error is same and why todoitems post is work?
@MasterJung, you should add {get; set;} to your property.
0

Can you make these changes and tell me what value is being returned on the variable result. Also, can you validate your ModelState first? [HttpPost] public async Task<ActionResult> PostAvatarChart( AvatarChart avatarChart) {

        await _context.AvatarCharts.AddAsync(avatarChart);
        var result = await _context.SaveChangesAsync() > 0;

        return CreatedAtAction("GetAvatarChart", new { modelId = avatarChart.modelId }, avatarChart);
    }

6 Comments

result is false public async Task<ActionResult<AvatarChart>> PostAvatarChart([FromBody] AvatarChart avatarChart) avatarChart value is null
Since he is using the ApiController attribute on top of the Controller, I think [FromBody] should have been handled automatically. I don't explicitly write [FromBody].
@MasterJung yeah, I suddenly find it's a api project, there is no need [FromBody].
JSON should have also worked fine, I don't know why the form's data is not being sent.
@mj1313 Could you please debug your code right from the start or post your project on some repo public? there must have been some missing.
|

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.