Angular: Getting a field is requested error for a non-null field

Discussão em 'Angular' iniciado por Roland Radványi, Abril 22, 2025.

  1. I try to create a webpage to my webshop and upload to the database

    OnSubmit(): void {
    const formData = new FormData();

    formData.append('Language', 'en');
    formData.append('Token', 'dfdfbsbfuefnbyxvfg6327jf843i');
    formData.append('Domain', 'localhost');

    formData.append('WebPageName', this.website.WebPageName);
    formData.append('Company_ID', this.website.CompanyId.toString());
    formData.append('CountryId', this.website.CountryId.toString());
    formData.append('LanguageId', this.website.LangId.toString() || '0');

    formData.append('CssBasics', this.website.CssBasic || '0');
    formData.append('HTMLCode', this.website.HTMLCode || '0');

    this.apiService.postMultipart('Domain/adddomain', formData).subscribe(
    res => {
    console.log('domain saved successfully:', res);
    this.loadWebPages();
    },
    err => console.error('Error saving domain:', err)
    );
    }


    It should load this DTO:

    public int ID { get; set; }
    public string WebPageName { get; set; }
    public string? Comments { get; set; }
    public DateTime? Created_At { get; set; }
    public DateTime? Updated_At { get; set; }
    public int Company_ID { get; set; }
    public int CountryId { get; set; }
    public int LanguageId { get; set; }
    public string? CssBasics { get; set; }
    public string? HTMLCode { get; set; }


    In my payload all the required fields are non-null, which looks good to me, but i getting this response image

    Currently I am fully out of ideas why this is happening since I named exactly the same and it should not be null

    Edit: The Controller part of the code:

    [HttpPost("adddomain")]
    public async Task<IActionResult> AddDomain([FromForm] WebPage domain)
    {
    domainModule = new BIDomain(1, _configuration);

    if (!ModelState.IsValid)
    {
    return BadRequest(ModelState);
    }

    try
    {
    // A ProductModule AddProductAsync metódusának meghívása
    bool result = await domainModule.AddDomainAsync(domain);

    if (result)
    {
    return Ok(new { Message = "Termék sikeresen hozzáadva!" });
    }

    return StatusCode(StatusCodes.Status500InternalServerError, "Nem sikerült hozzáadni a terméket.");
    }
    catch (Exception ex)
    {
    // Hiba esetén 500-as válasz
    return StatusCode(StatusCodes.Status500InternalServerError, new { Error = ex.Message });
    }
    }

    Continue reading...

Compartilhe esta Página