0

我们可以在 Blazor 平台中借助 typeparam 分配和重用 TValue。但是,我们将如何为 Blazor InputNumber 组件动态分配 TValue?

代码示例:

[索引.剃刀]

<EditForm>

      @*The below definiton is working*@
      <InputNumber TValue="int?" @bind-Value="@DynamicModelInstance.ValueAsT"></InputNumber>
       @*The below definiton is not working*@
    <InputNumber TValue="DynamicModelInstance.Type" @bind-Value="@DynamicModelInstance.ValueAsT"></InputNumber>
</EditForm>
@code {
    public DynamicModel<int> DynamicModelInstance { get; set; }

    protected override void OnInitialized()
    {
        DynamicModelInstance = new DynamicModel<int>();
        DynamicModelInstance.ValueAsT = 500;
    }
}

[动态模型.cs]

namespace CustomComponent.Pages
{
public class DynamicModel<T> where T : struct
{
    public System.Type Type { get; set; }
    public bool Enabled { get; set; }

    public DynamicModel()
    {
        this.Type = typeof(T);
    }

    private T _value;

    public T ValueAsT
    {
        get { return (T)_value; }
        set { this._value = value; }
    }
}
}

如何达到这个要求?

4

1 回答 1

0

问题是您的 EditForm:

EditForm 需要 Model 参数或 EditContext 参数

<EditForm Model="dynamicModelInstance">
    <InputNumber @bind-Value="@dynamicModelInstance!.Value" />
</EditForm>
@code {
    public DynamicModel<int>? dynamicModelInstance;

    protected override void OnInitialized()
    {
        dynamicModelInstance = new()
        {
            Value = 500
        };
    }
}
public class DynamicModel<T> where T : struct
{
    public T Value { get; set; }
}

于 2021-12-08T17:07:07.957 回答