-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Is there an existing issue for this?
- 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:
<InputText readonly @bind-Value=Model.FamilyName/>
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
<InputText readonly Value=@Model.Age/>
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
<InputText readonly Value=@Model.Age ValueExpression=@(() => 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-
<InputText readonly @bind-Value=@Model.Age @bind-Value:oneway />
<InputText readonly @bind-Value=@Model.Age @bind-Value:readonly />
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.
- Easily discoverable for screen readers.
- User can tab to them.
- Easy to select-all and copy to clipboard.
- They truncate long content, whilst still allowing the user to scroll through and read it.