Skip to content

ModelBinder - Model becomes NULL if only One of its properties is invalid #9856

Closed
@mdmoura

Description

@mdmoura

Describe the bug

Model becomes NULL if only ONE of its properties is invalid

To Reproduce

Using ASP.NET Core 2.2 I have, on an API Project, the following Controller:

public class BookController : Controller {

  public async Task<IActionResult> Create([FromBody]BookModel model) {
      // Validate and Create book      
  }

}

The BookModel is:

public class BookModel { 
  public String Title { get; set; }
  public DateTime? PublishedAt { get; set; }       
}

I called the action with an invalid PublishedAt date:

{
  "Title": "book title",
  "PublishedAt": "2019-04-32"
}

In this case, on my action, model is NULL even if Title is defined and is valid.

If I call the action with a valid PublishedAt date or NULL then the model is not null.

Expected behavior

Shouldn't model in action be defined even if one of its properties fails to bind?

Additional context

.NET Core SDK (reflecting any global.json):
Version: 2.2.104
Commit: 73f036d4ac

Runtime Environment:
OS Name: Mac OS X
OS Version: 10.14
OS Platform: Darwin
RID: osx.10.14-x64
Base Path: /usr/local/share/dotnet/sdk/2.2.104/

Host (useful for support):
Version: 2.2.2
Commit: a4fd7b2c84

.NET Core SDKs installed:
2.2.104 [/usr/local/share/dotnet/sdk]

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: By DesignResolved because the behavior in this issue is the intended design.area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions