© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
51 replies
cumslvt13

✅ Binding array with default value doesn't work

I've got the following model and controller
public class MyModel
{
    public int? Test { get; set; } = 10;
    public IEnumerable<string> Sections { get; set; } = Array.Empty<string>();
}

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    [HttpGet]
    public ActionResult<MyModel> Test([FromQuery] MyModel model)
    {
        return Ok(model);
    }
}
public class MyModel
{
    public int? Test { get; set; } = 10;
    public IEnumerable<string> Sections { get; set; } = Array.Empty<string>();
}

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    [HttpGet]
    public ActionResult<MyModel> Test([FromQuery] MyModel model)
    {
        return Ok(model);
    }
}

The
https://localhost:5001/test?Test=3&Sections=USER
https://localhost:5001/test?Test=3&Sections=USER
resolves to
{"test":3,"sections":[]}
{"test":3,"sections":[]}
, but if I remove default value for a
Sections
Sections
property it resolves to
{"test":3,"sections":["USER"]}
{"test":3,"sections":["USER"]}
. Why behavior is different from int default value?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

❔ MAUI IsVisible Binding doesn't work
C#CC# / help
3y ago
❔ Entry Binding in custom control doesn't work
C#CC# / help
3y ago
Value binding
C#CC# / help
4y ago
MySqlException: Field 'CreatedOnUtc' doesn't have a default value
C#CC# / help
2y ago