Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如何将列表作为参数传递给blazor页面_List_Url_Parameters_Routes_Blazor - Fatal编程技术网

List 如何将列表作为参数传递给blazor页面

List 如何将列表作为参数传递给blazor页面,list,url,parameters,routes,blazor,List,Url,Parameters,Routes,Blazor,我试图通过点击按钮将两个blazor页面链接在一起。我想将字符串列表作为参数传递给另一个页面 现在,我已尝试在所需页面上添加路线: @page "/page1/{filterValues}" 在代码部分,我定义参数如下: [Parameter] public IEnumerable<String> filterValues { get; set; } 然而,这似乎不起作用。url如下所示: https://localhost:5001/page1/System

我试图通过点击按钮将两个blazor页面链接在一起。我想将字符串列表作为参数传递给另一个页面

现在,我已尝试在所需页面上添加路线:

@page "/page1/{filterValues}"
在代码部分,我定义参数如下:

[Parameter]
public IEnumerable<String> filterValues { get; set; }
然而,这似乎不起作用。url如下所示: https://localhost:5001/page1/System.Collections.Generic.List%601[System.Object].Cast()

我在控制台中得到这个错误:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Unable to set property 'filterValues' on object of type 'Proj.Client.Pages.page1'. The error was: Specified cast is not valid.
System.InvalidOperationException: Unable to set property 'filterValues' on object of type 'Proj.Client.Pages.page1'. The error was: Specified cast is not valid.

有人能解释一下如何通过url将字符串列表作为参数传递吗?

您可以序列化列表:

调用组件
<a href="/somepage/@param1/@param2/@param3/@param4">Some Page</a>

@code {
    string[] list1 = new[] { "Id1", "FirstName1", "Surname1" };
    string[] list2 = new[] { "Id2", "FirstName2", "Surname2", "Value.value", "Value", "Value" };
    string[] list3 = new[] { "Id3", "FirstName3", "Surname3", "Value/value", "Value", "Value", "Value", "Value" };
    string[] list4 = new[] { "Id4", "FirstName4", "Surname4", "Value-value", "Value", "Value", "Value" };

    public string param1 => Serialize(list1);
    public string param2 => Serialize(list2);
    public string param3 => Serialize(list3);
    public string param4 => Serialize(list4);

    static string Serialize(string[] source) => 
        System.Net.WebUtility.UrlEncode(System.Text.Json.JsonSerializer.Serialize(source));
}


这个字符串列表会有多大?最后我想发送4个不同的列表。最大的一个是大约8到10个条目。谢谢,这很有帮助。你知道我如何使这些参数成为可选的吗?例如,有一次我只想传递List1和List4,另一次我想再次传递它们所有的内容。@Snoopy111将列表包装在一个类中并序列化它。将其作为单个参数传递。
<a href="/somepage/@param1/@param2/@param3/@param4">Some Page</a>

@code {
    string[] list1 = new[] { "Id1", "FirstName1", "Surname1" };
    string[] list2 = new[] { "Id2", "FirstName2", "Surname2", "Value.value", "Value", "Value" };
    string[] list3 = new[] { "Id3", "FirstName3", "Surname3", "Value/value", "Value", "Value", "Value", "Value" };
    string[] list4 = new[] { "Id4", "FirstName4", "Surname4", "Value-value", "Value", "Value", "Value" };

    public string param1 => Serialize(list1);
    public string param2 => Serialize(list2);
    public string param3 => Serialize(list3);
    public string param4 => Serialize(list4);

    static string Serialize(string[] source) => 
        System.Net.WebUtility.UrlEncode(System.Text.Json.JsonSerializer.Serialize(source));
}

@page "/somepage/{List1Json}/{List2Json}/{List3Json}/{List4Json}"

<h3>SomePage</h3>

<ul>
    @foreach (var item in List1) { <li>@item</li> }
</ul>
<ul>
    @foreach (var item in List2) { <li>@item</li> }
</ul>
<ul>
    @foreach (var item in List3) { <li>@item</li> }
</ul>
<ul>
    @foreach (var item in List4) { <li>@item</li> }
</ul>
@code {
    [Parameter] public string List1Json { get; set; }
    [Parameter] public string List2Json { get; set; }
    [Parameter] public string List3Json { get; set; }
    [Parameter] public string List4Json { get; set; }

    public string[] List1 => Deserialize(List1Json);
    public string[] List2 => Deserialize(List2Json);
    public string[] List3 => Deserialize(List3Json);
    public string[] List4 => Deserialize(List4Json);

    static string[] Deserialize(string source) 
        => System.Text.Json.JsonSerializer.Deserialize<string[]>(System.Net.WebUtility.UrlDecode(source));
}