dotnet / dotnet/aspnetcore

BindProperty silently fails when used with a "private set" property

Open
#43,368 1 comment 1 reaction 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

This code does not bind the value of `Answer`; it remains 0 no matter what the value of the `Answer` POST parameter is:

```csharp
[BindProperty]
public int Answer { get; private set; }
```

Everything works fine when removing the `private`.

### Expected Behavior

I would like to see an error message if `[BindProperty]` is applied to a property with a private setter. Preferably, this would be a compile-time error, but even a run-time error would be helpful. As long as it doesn't fail silently.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

6.0.400

### Anything else?

```
.NET SDK (reflecting any global.json):
Version: 6.0.400
Commit: 7771abd614

Runtime Environment:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: [redacted]

global.json file:
Not found

Host:
Version: 6.0.8
Architecture: x64
Commit: 55fb7ef977

.NET SDKs installed:
6.0.400

.NET runtimes installed:
Microsoft.AspNetCore.App 5.0.17
Microsoft.AspNetCore.App 6.0.3
Microsoft.AspNetCore.App 6.0.8
Microsoft.NETCore.App 5.0.17
Microsoft.NETCore.App 6.0.3
Microsoft.NETCore.App 6.0.8
Microsoft.WindowsDesktop.App 5.0.17
Microsoft.WindowsDesktop.App 6.0.8
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.