Reputation: 1554
I'm trying to use 2-way binding between a form and my object. I can get it to work for InputText
. But i get this errors when i add the ValueChanged
attribute for InputNumber
:
CS1662 Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
CS1503 Argument 2: cannot convert from 'method group' to 'EventCallback'
When i compile this Component.razor
page:
@page "/test"
@using System.ComponentModel.DataAnnotations
<EditForm Model="@model">
<DataAnnotationsValidator />
<ValidationSummary />
<InputText Value="@model.Street" ValueExpression="@(() => model.Street)" ValueChanged="@StreetChanged" />
<InputNumber Value="@model.HouseNr" ValueExpression="@(() => model.HouseNr)" ValueChanged="@HouseNrChanged" />
<button type="submit">Send</button>
</EditForm>
@code {
protected void StreetChanged(string? value)
{
// ... code to lookup address and fill other fields
model.Street = value;
}
protected void HouseNrChanged(int? value)
{
// ... code to lookup address and fill other fields
model.HouseNr = value;
}
private Address model { get; set; } = new();
class Address
{
[Required]
public string? Street { get; set; }
[Required]
public int? HouseNr { get; set; }
}
}
I've tried to changing my HouseNrChanged
method, but i can't figure it out. It seems to have something to do with the different types of the ValueChanged
properties:
EventCallback<string>
EventCallback<TValue>
Questions:
It's very hard to find examples
Upvotes: -1
Views: 1796
Reputation: 11302
It's because the type of the generic EventCallback<TValue>
cannot be inferred. You have to set the TValue
property on the InputNumber
<InputNumber TValue="int?" Value="@model.HouseNr" ValueExpression="@(() => model.HouseNr)" ValueChanged="@HouseNrChanged" />
Upvotes: 1