如何通过Blazor中的下拉框触发带参数的方法?

huangapple go评论71阅读模式
英文:

How to trigger a method with parameter through a dropdown in Blazor?

问题

I would like to trigger a method through a dropdown list in my editform. The method should be fired when I change the value in my dropdown list. But it seems not to work, anyone know what I'm doing wrong?

My code in UI.

<EditForm Model="@exampleModel">
   <p>
        <label>
            Name:
            <InputSelect @onchange="() => GetNames(name)" @bind-Value="name">
                @foreach (string item in names)
                {
                    <option value="name">@item</option>
                }
            </InputSelect>
        </label>
    </p>
<EditForm />

My code

@code {
    public string? name { get; set; }

    public async Task GetNames(string name)
    {
        // Get Names
    }

    string[] names = new string[] { "test", "test1", "test2", "test3", "test4" };
}
英文:

I would like to trigger a method through a dropdown list in my editform. The method should be fired when i change the value in my dropdownlist. But it seems not to work, anyone know what i doing wrong?

My code in UI.


<EditForm Model="@exampleModel">
   <p>
        <label>
            Name:
            <InputSelect @onchange="() => GetNames(name)" @bind-Value="name">
                @foreach (string item in names)
                {
                    <option value="name">@item</option>
                }
            </InputSelect>
        </label>
    </p>
<EditForm />

My code

@code {

public string? name{ get; set; }

  public async Task GetNames(string name)
 {
   // Get Names
 }

    string[] names = new string[] { "test", "test1", "test2", "test3", "test4" };



}

答案1

得分: 2

以下是您提供的代码的中文翻译部分:

尝试:

    <EditForm Model="@exampleModel" >
    
       <p>
            <label>
                名称:
                <Select @onchange="GetNames" >
                    @foreach (var item in names)
                    {
                        <option value="@item">@item</option>
                    }
                </Select>
            </label>
        </p>
    </EditForm>
    @code {
            private ExampleModel exampleModel = new();
            public string? name { get; set; }
    
            
        public async Task GetNames(ChangeEventArgs e)
        {
            // 获取名称
            name = e.Value.ToString();
        }
            string[] names = new string[] { "test", "test1", "test2", "test3", "test4" };
    }

希望这对您有所帮助。如果您需要进一步的翻译或其他帮助,请随时提问。

英文:

Try:

<EditForm Model="@exampleModel" >

   <p>
        <label>
            Name:
            <Select @onchange="GetNames" >
                @foreach (var item in names)
                {
                    <option value="@item">@item</option>
                }
            </Select>
        </label>
    </p>
</EditForm>
@code {
        private ExampleModel exampleModel = new();
        public string? name { get; set; }

        
    public async Task GetNames(ChangeEventArgs e)
    {
        // Get Names
        name = e.Value.ToString();
    }
        string[] names = new string[] { "test", "test1", "test2", "test3", "test4" };



    }

result:

如何通过Blazor中的下拉框触发带参数的方法?

答案2

得分: 1

这演示了两种不同的方法,取决于你使用的DotNet版本:

@page "/"

<PageTitle>Index</PageTitle>

<EditForm Model=_model>
    <h3>Net7.0+</h3>
    <div class="mb-3">
        <label class="form-label"> 姓名:</label>
        <InputSelect class="form-select" @bind-Value="@_name" @bind-Value:after=GetNames>
            @if (_name is null)
            {
                <option selected disabled value="">-- 选择一个姓名 --</option>
            }
            @foreach (string item in names)
            {
                <option value="@item">@item</option>
            }
        </InputSelect>
    </div>
    <h3>Net6.0+</h3>
    <div class="mb-3">
        <label class="form-label"> 姓名:</label>
        <InputSelect class="form-select" TValue=string Value=_name ValueChanged=this.SetName ValueExpression="() => _name">
            @if (_name is null)
            {
                <option selected disabled value="">-- 选择一个姓名 --</option>
            }
            @foreach (string item in names)
            {
                <option value="@item">@item</option>
            }
        </InputSelect>
    </div>
</EditForm>

<div class="bg-dark text-white m-2 p-2">
    <pre>结果 =  @_result</pre>
</div>

@code {
    private string[] names = new string[] { "test", "test1", "test2", "test3", "test4" };
    private Model _model = new();
    public string? _name { get; set; }
    private string? _result;

    public async Task GetNames()
    {
        await Task.Delay(500);
        _result = $" {DateTime.Now.ToLongTimeString()} 找到 {_name}";
    }

    public async Task SetName(string? value)
    {
        _name = value;
        await GetNames();
    }

    public class Model
    {
    }
}
英文:

This demonstrates doing it in two different ways depending on which version of DotNet you're using:

@page "/"

<PageTitle>Index</PageTitle>

<EditForm Model=_model>
    <h3>Net7.0+</h3>
    <div class="mb-3">
        <label class="form-label"> Name:</label>
        <InputSelect class="form-select" @bind-Value="@_name" @bind-Value:after=GetNames>
            @if (_name is null)
            {
                <option selected disabled value="">-- Select a Name --</option>
            }
            @foreach (string item in names)
            {
                <option value="@item">@item</option>
            }
        </InputSelect>
    </div>
    <h3>Net6.0+</h3>
    <div class="mb-3">
        <label class="form-label"> Name:</label>
        <InputSelect class="form-select" TValue=string Value=_name ValueChanged=this.SetName ValueExpression="() => _name">
            @if (_name is null)
            {
                <option selected disabled value="">-- Select a Name --</option>
            }
            @foreach (string item in names)
            {
                <option value="@item">@item</option>
            }
        </InputSelect>
    </div>
</EditForm>

<div class="bg-dark text-white m-2 p-2">
    <pre>Result =  @_result</pre>
</div>

@code {
    private string[] names = new string[] { "test", "test1", "test2", "test3", "test4" };
    private Model _model = new();
    public string? _name { get; set; }
    private string? _result;

    public async Task GetNames()
    {
        await Task.Delay(500);
        _result = $"Found {_name} at {DateTime.Now.ToLongTimeString()}";
    }

    public async Task SetName(string? value)
    {
        _name = value;
        await GetNames();
    }

    public class Model
    {
    }
}

huangapple
  • 本文由 发表于 2023年6月15日 14:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定