页面无法呈现,但没有错误消息

时间:2018-12-21 19:11:30

标签: blazor

VS 2017内部版本15.9.3 Blazor运行时0.7 rtm
在Blazor上试一试。有一个简单的页面,我正在尝试获取一个下拉列表,以填充对象列表中的项目。项目生成,但页面不呈现,也没有生成任何错误或异常。其他页面工作正常,但不是这一页。选择显然很重要,因为如果我删除它,它会很好地呈现。但是,在浏览器或Visual Studio中都看不到错误。我正在使用F5从VS启动,因此可以根据需要进行调试。

@page "/PriceList"


<p>Audit screen test</p>

<select id="selPriceRate" bind="@prices">
    <option value="test">test</option>
</select>

<br/>
Select terminal:<br/>
<select id="ddTerminal" onchange="@ChangeTerminal" >
    <option value="NYC">NYC</option>
    <option value="LHR">LHR</option>
    <option value="DFW">DFW</option>
    <option value="LAX">LAX</option>
</select>

@functions {

    List<PriceSetting> prices = new List<PriceSetting>();

    void ChangeTerminal()
    {
        // load ddRateMaster
        prices.Add(new PriceSetting() { PriceName = "test1",PriceValue = 104.13m });
        prices.Add(new PriceSetting() { PriceName = "test2", PriceValue = 105.13m });
        prices.Add(new PriceSetting() { PriceName = "test3", PriceValue = 106.13m });
    }

    protected override async Task OnInitAsync()
    {
        var t = prices.Count();

        await Task.Delay(1000);

    }

}

2 个答案:

答案 0 :(得分:1)

您刚刚尝试绑定价格表

List<PriceSetting> prices = new List<PriceSetting>();

选择元素值。

<select id="selPriceRate" bind="@prices">
<option value="test">test</option>
</select>

那是不正确的。 您可以尝试这样的事情

<select id="selPriceRate" bind="@selectedPriceName">
@foreach(var price in prices) {
    <option value="@price.PriceName">@price.PriceName</option>
}    
</select>

<select id="selPriceRate" onchange="@ChangePriceRate">
@foreach(var price in prices) {
    <option value="@price.PriceName">@price.PriceName</option>
}    
</select>

注意:您不能在同一元素上使用bind和onchange @bind and @onchange on the same element doesn't work

答案 1 :(得分:1)

此答案基于您要创建两个选择元素的假设。一 显示机场列表,第二个显示价格列表。 (这是我能从您的问题中得出的最好的答案) 现在,当用户选择机场时,您要第二个选择列出价格,对吗?

@if (Terminals == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <select bind="@SelectedTerminalId">
        <option value=@(0)></option>
        @foreach (var Terminal in Terminals)
        {
            <option value="@Terminal.TerminalId">@Terminal.Name</option>
        }
    </select>
}

@if (SelectedTerminalId != default)
{
    var prices = Terminal.Single(x => x.TerminalId == SelectedTerminalId).Prices;

    <select bind="@SelectedPriceSettingId">
        <option value=@(0)></option>
        @foreach (var price in Prices)
        {
            <option value="@price.Id">@price.Name (@price.Value)</option>
        }
    </select>

}

@functions {
    private static readonly List<Terminal> Terminals = new List<Terminal>{
            new Terminal{
                TerminalId = 1, Name = "NYC", Prices = new List<PriceSetting>
                {
                    new PriceSetting{Id = 1, Name = "test1", Value = 104.13m},
                    new PriceSetting{Id = 2, Name = "test2", Value = 105.13m )
                }
            },
            new Terminal{
                TerminalId = 2, Name = "LHR", Prices = new List<PriceSetting>
                {
                    new PriceSetting{Id = 3, Name = "test3", Value = 106.13m}

                }
            }
        };


    int _selectedTerminalId;
    int SelectedTerminalId
    {
        get => _selectedTerminalId;
        set
        {
            _selectedTerminalId = value;
            SelectedPriceSettingId = default;
        }
    }

    int SelectedPriceSettingId { get; set; }


}