Eugene Sukh
Eugene Sukh

Reputation: 2737

ASP.NET core return Bad object from controller

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.

Upvotes: 0

Views: 336

Answers (1)

Mohsin Mehmood
Mohsin Mehmood

Reputation: 4236

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.

Upvotes: 2

Related Questions