Blazor: One-way binding with automatic expression
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
`@bind-Value=Expression` will automatically set `Value`, `ValueChanged`, and `ValueExpression`. This is a nice shortcut.
I like to use input controls for readonly data. (Reasons in Additional Context section). So, I would typically do this:
```
```
This is fine, but when I want to display a derived property
```
public int Age => (Calculate age from date of birth)
```
I cannot use `@bind` because the property cannot be set. The next thing to try would be
```
```
But that gives the exception
> InvalidOperationException: Microsoft.AspNetCore.Components.Forms.InputText requires a value for the 'ValueExpression' parameter. Normally this is provided automatically when using 'bind-Value'.
Microsoft.AspNetCore.Components.Forms.InputBase.SetParametersAsync(ParameterView parameters)
So, finally I end up with
```
Model.Age)/>
```
But this is repetitive, and when your model has many properties with similar names it is easy to bind to a different property to the one being passed as an expression. This is likely in some domains where property names are very similar (Fld102, Fld201).
And the expression is needed because, even though the property is readonly, it might have validation associated with it that must be displayed to the user...
```
[Range(18, int.MaxValue, ErrorMessage = "Age must be at least 18")]
```
### Describe the solution you'd like
Add a `oneway` or `readonly` directive for `@bind-`
```
```
```
```
The code generator wouldn't generate code to set the value, but it would set `Value` and `ValueExpression`.
### Additional context
Reasons to use readonly input controls.
1. Easily discoverable for screen readers.
2. User can tab to them.
3. Easy to select-all and copy to clipboard.
4. They truncate long content, whilst still allowing the user to scroll through and read it.
Contributor guide
Assessment
This issue has not been assessed yet.