C#C
C#3y ago
Utsuhoagie

✅ Nullable DateTimeOffset field in a [FromForm] model is always null

I have a React Native (Expo running on Expo Go on an Android specifically) client and an ASP.NET Core Web API server.
My client sends requests with content type multipart/form-data, and supplies the correct values for each field. Specifically in this example, the BirthDate field is either omitted entirely from the FormData, or it's given an ISO timestamp string with Z offset. I don't send
null
, undefined, '' or anything like that.
But when the request reaches the server, the model's nullable BirthDate field is always null with either cases (client doesn't include the field in its FormData, OR the field's value is something like 2023-04-27T02:50:23.636Z). Other fields are still sent properly (either given a proper value, or is
null
once the server reads the request).

public class UpdateSelfRequest
{
    public string NationalId { get; set; } = string.Empty;
    public string? Phone { get; set; }
    public string? Address { get; set; }
    public DateTimeOffset? BirthDate { get; set; }
    public IFormFile? Image { get; set; }
}

[HttpPut("UpdateSelf/{NationalId}")]
[Authorize(Roles = AuthRoles.Employee)]
[Consumes("multipart/form-data")]
public async Task<IActionResult> UpdateSelf(
    [FromRoute] string NationalId,
    [FromForm] UpdateSelfRequest req)
{
    // -----> Here, req.BirthDate is null despite the client sending a value
    var result = await _service.UpdateSelf(NationalId, req);
    if (!result.Success)
    {
            return Forbid();
    }

    return Ok();
}


Logs in screenshot:
image.png
Was this page helpful?