Closed conficient closed 5 years ago
Thanks for contacting us, @conficient . You can write it this way, and the changes would propagate.
<InputText Value="@Value" ValueChanged="@ValueChanged" />
Alternatively, you can simply subclass from the InputText
component.
I tried the approach you suggested but this results in a WASM error:
WASM: System.InvalidOperationException: Microsoft.AspNetCore.Components.Forms.InputText requires a value for the 'ValueExpression' parameter. Normally this is provided automatically when using 'bind-Value'
I think this is because the ValueChanged event handler isn't set?
I resolved my issue anyway once I realised (thanks to @joshlang ) that I needed to invoke the ValueChanged
from the Value
property setter.
I will push the updated version on this code to my repo. This issue can remain closed though.
If your goal is to wrap up an InputText
in some bootstrap styling, I would suggest subclassing. The form types are designed for this use case.
If you're really determined to compose, then accept an Expression<Func<string>> ValueExpression
as a parameter as well and pass that through also.
Thanks @rynowak - yes I wanted to get rid of boilerplate <div class="form-group"><label>....
around each input control so my forms are simpler. I'll try what you suggest.
I started to see the following error after upgrading from preview5 to preview6
WASM: System.InvalidOperationException: Microsoft.AspNetCore.Components.Forms.InputText requires a value for the 'ValueExpression' parameter. Normally this is provided automatically when using 'bind-Value'
this is my custom component the razor file
@inherits CcInputTextComponent
@using Microsoft.AspNetCore.Components
<input type="@Type"
class="@CssClass"
id="@Id"
value="@BindMethods.GetValue(CurrentValue)"
onchange="@BindMethods.SetValueHandler(__value => CurrentValue = __value, CurrentValue)"
placeholder="@PlaceHolder"
/>
the "code behind" - I prefer not to use the @ code{} I like the idea of having the csharp completely separated from the razor
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.RenderTree;
namespace BzTest.Shared.CcInputText
{
public class CcInputTextComponent: InputText
{
[Parameter] public string Type { get; set; } = "text";
[Parameter] public string PlaceHolder { get; set; }
}
}
the use of the component
<CcInputText id="Password" Type="password" bind-Value="@loginRequest.Password" Class="form-control input_pass" PlaceHolder="Password"></CcInputText>
use @bind-Value instead of bind-Value. See the preview6 upgrade notes for more details
Yep, that seems to work. I get a red squiggle but might be Resharper's fault.
I don't know looks weird, not the squiggle, the syntax. The @ before the attribute name, I don't know it's different from the rest of the Razor syntax, maybe just have to get used to.
For those that end up here like me, it appears as though "@bind-Value" became case sensitive at some point.
Yes it did. Which now allows us to create components like
Describe the bug
Binding values using
@bind-XXX
binding syntax does not work if nested.Demo
To Reproduce
Steps to reproduce the behavior:
@bind-Value
syntax in theShared
folderEditForm
in theIndex.razor
page and a model to bind toInputText
component, bind using@bind-Value
to one model property@bind-Value
Expected behavior
Expected the binding to work with nested controls, so both properties of the model are updated when the input control is changed.
Actual behavior
A direct binding with
InputText
works but the custom control binding does not bind to the model. It binds when initially created but updates to the control do not get passed back the to page model.Sample code
Example repo created at https://github.com/conficient/BlazorBug04
MyControl.razor
Index.razor
Additional context
Previously reported on Blazor repo: #1490 Also reported in aspnetcore repo: #5504 Related: #6351 was supposed to fix?
Include the output of
dotnet --info