0

I have repo method, that update entry in database. Here is code from repo method

 public async Task<string> UpdateProfile(string email, string firstname, string lastname, DateTime birthday)
    {
        string result;
        var user = _context.AspNetUsers.Where(x => x.Email == email).FirstOrDefault();
        user.FirstName = firstname;
        user.LastName = lastname;
        user.Birthday = birthday;
        await _context.SaveChangesAsync();
        result = "Updated";
        return result;
    }

And here is how I call it from controller

[HttpPost]
    public JsonResult UpdateProfile([FromBody] ProfileViewModel profile)
    {
        var result = _profile.UpdateProfile(profile.Email, profile.FirstName, profile.LastName, profile.Birthday);
        return Json(result);
    }

But in postman I see Bad object, but entry is updated.

Why I get this and how I can fix this?

Thank's for help.

2
  • Do you mean bad object or Bad Request? Commented Sep 19, 2018 at 19:40
  • Shouldn't you be awaiting the task? Otherwise your controller method will hit the return statement before result is assigned by the async callback. Try var result = await _profile.UpdateProfile(profile.Email, profile.FirstName, profile.LastName, profile.Birthday); Commented Sep 19, 2018 at 19:42

1 Answer 1

2

Update method like this:

[HttpPost]
    public async Task<IActionResult> UpdateProfile([FromBody] ProfileViewModel profile)
    {
        var result = await _profile.UpdateProfile(profile.Email, profile.FirstName, profile.LastName, profile.Birthday);
        return Ok(result);
    }

Changed return type to IActionResult and also made the controller action async.

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

1 Comment

Yeah, but you have error . public async Task<IActionResult> is correct one

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.