-1

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:

  • InputText.ValueChanged has type EventCallback<string>
  • InputText.ValueChanged has type EventCallback<TValue>

Questions:

  • Any ideas on how to fix this?
  • Is there any documentation?
  • What does ValueExpression do besides causing a runtime error when it is omitted?

It's very hard to find examples

symbiont
  • 1,428
  • 3
  • 21
  • 27
  • Does this answer your question? [Blazor - cannot convert from 'method group' to 'EventCallback'](https://stackoverflow.com/questions/64308947/blazor-cannot-convert-from-method-group-to-eventcallback) – Dimitris Maragkos Sep 08 '22 at 14:28
  • @DimitrisMaragkos i read that earlier, but i didn't see how it is similar at the time – symbiont Sep 08 '22 at 15:19

1 Answers1

1

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" />
Dimitris Maragkos
  • 8,932
  • 2
  • 8
  • 26