Results 1 to 3 of 3

Thread: when pass id value of put(update) from postman it passed as 0 although it have value

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    when pass id value of put(update) from postman it passed as 0 although it have value


    problem


    when pass id value of put(update) from postman it passed as 0 although it have value on link ?

    i select key from post man as contenttype application/json

    Code:
    and this is my url
    
    https://localhost:44326/api/Employee/put?id=5
    and when put breakpoint in function put and run app

    it hit breakpoint but id return with 0 although i have 5 in my link

    and 5 also exist on database

    Code:
    [Produces("application/json")]
        [Route("api/Employee")]
        public class EmployeeController : Controller
        {
    [HttpPut("{id}")]
            public IActionResult PutEmployee(int id, [FromBody] Employee employee)
            {
            
           
             
                    if (id != employee.EmployeeId)
                {
                    return BadRequest();
                }
    
               
    
                try
                {
                    _repository.Update(employee);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EmployeeExists(id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
    
                return Ok();
            }
    when run API update from angular 6 it give me bad request because id is passing by 0 ?

    why is pass id as 0 and how to solve this problem ?

    I work with asp.net core 2.1 visual studio 2017

    and I have this function above on controller Employee

    this is only function update (put) for my controller

    so that how to solve problem passing 0 and

    what is correct link if above is wrong ?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: when pass id value of put(update) from postman it passed as 0 although it have va

    How did you verify that the id was 0? There's nothing there that checks to see if id = 0 ... it checks to see if id != employee.EmployeeId .... so my question is, was employee filled correctly? Was the .EmployeeId correct? Did it match id? How did you discover that id was 0?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: when pass id value of put(update) from postman it passed as 0 although it have va

    Is the Employee object populated or is that also blank?

    HAve you tried putting the id into the url rather than a querystring? e.g. https://localhost:44326/api/Employee/put/5 or even https://localhost:44326/api/Employee/5 - I am not entirely sure the /put is required. Unfortunately I don't have VS handy to test it out at the moment though so I am going on a vague memory and guesswork....

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width