如何在Blazor中获得级联价值?

时间:2019-08-26 10:10:08

标签: forms blazor cascadingdropdown

我有一个表单,其中用户首先在下拉列表中选择一家公司,然后基于此表单,他们可以从下拉列表中选择一个项目。从数据库中查询的项目基于用户和公司。

我尝试遵循有关cascadingdropdownlist的较旧的文章:https://www.mikesdotnetting.com/article/320/improved-cascading-dropdowns-with-blazor,但这已经很老了,并且似乎与当前文档有很大不同。去年,Blazor发生了很多事情。在阅读当前的Blazor官方文档(https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#cascading-values-and-parameters)时,我看到他们有一个名为“ CascadingValue”的“ HTML”标签,这对我来说是正确的方法吗?如果可以的话,请让我给我看一个如何做的例子?

<EditForm Model="@expense" Context="formContext" OnValidSubmit="@HandleValidSubmit">
                    <DataAnnotationsValidator />
                    <ValidationSummary />
<div style="float:left;width: 40%;">
                        <label asp-for="Client" class="control-label">Selskap</label>
                        <select asp-for="Client" class="form-control" @bind="@expense.Client">
                            <option></option>

                            @foreach (var ExpClient in cList)
                            {
                                <option selected="selected">
                                    @ExpClient.client
                                </option>
                                <option value="@ExpClient.client">@ExpClient.client</option>
                            }
                        </select>
                    </div>
                    <div style="clear:both;">&nbsp;</div>
                    <div style="float:left;width: 50%;">
                        <label asp-for="Project" class="control-label">Prosjekt</label>
                        <select asp-for="Project" class="form-control" @bind="@expense.Project">
                            <option></option>
                            @foreach (var ExpPro in ProList)
                            {
                                <option selected="selected">
                                    @ExpPro.Project
                                </option>
                                <option value="@ExpPro.Project">@ExpPro.Project</option>
                            }
                        </select>
                    </div>
</EditForm>

@code{

protected void GetExpProList()
    {
               ProList = objexpense.GetExpProList(userN).ToList();
    }
}

现在它仅基于用户名获取项目,这可行,但是我  还需要获取选定的客户端,以便将usernN和client都传递给查询数据库的方法。

如果有人能在正确的方向指我,我将不胜感激。

编辑: 根据我得到的答案,我的解决方案最终看起来像这样,并且还受到Mikesdotnetting(https://www.mikesdotnetting.com/article/320/improved-cascading-dropdowns-with-blazor)的影响:

        <InputSelect id="Client" class="form-control" @bind-Value="@expense.Client">
                        <option value=@(0)></option>

                        @foreach (var ExpClient in cList)
                {

                        <option value="@ExpClient.client">@ExpClient.client</option>
                }
                    </InputSelect>
                </div>
                @if (expense.Client != default ){
                <div style="clear:both;">&nbsp;</div>
                <div style="float:left;width: 50%;">

                    <label id="Project" class="control-label">Prosjekt</label>
                    <InputSelect id="Project" class="form-control" @bind-Value="@expense.Project">
                        <option value=@(0)></option>
                        @foreach (var ExpPro in objexpense.GetExpProList(userN,expense.Client).ToList())
                {

                        <option value="@ExpPro.Project">@ExpPro.Project</option>
                }
                    </InputSelect>
                </div>
                }

1 个答案:

答案 0 :(得分:3)

有一个名为InputSelect的组件,您可以在该组件上绑定一个值。

<InputSelect @bind-Value="selectedClient" class="form-control">
    @foreach (var ExpClient in cList)
    {
        <option value="@ExpClient.client">@ExpClient.client</option>
    }
</InputSelect>

第二个下拉菜单如下:

<InputSelect @bind-Value="selectedProject" class="form-control">
    @foreach (var ExpPro in objexpense.GetExpProList(userN, selectedClient).ToList())
    {
        <option value="@ExpPro.Project">@ExpPro.Project</option>
    }
</InputSelect>