如何从一个 `Selectlist` 中获取多个选定的值?

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

How get multi selected values from a `Selectlist`?

问题

如何从 Selectlist 获取多个已选择的值?

以下是我迄今为止尝试的内容:以下代码仅获取一个值,所以问题可能出在这一行 Colors = Request.Query["Colors"];

<form asp-page="index" method="get">
    <select asp-for="Colors" asp-items="@Model.Colors_SELECT" class="MultiSelect" multiple>
        <option value="">All</option>
    </select>
</form>

URL 示例:
https://localhost:7221/Index?Colors=&Blk&Colors=red&Colors=blue

后端代码:

[BindProperty(SupportsGet = true)]
public string? Colors { get; set; }
public SelectList? Colors_SELECT { get; set; }

public async Task OnGetAsync()
{
    // 填充选择值
    Colors_SELECT = new SelectList(_services.Fill_Colors(), "Value", "Text", 1);

    // 选定的值
    Colors = Request.Query["Colors"];
}

// 用于填充颜色的辅助函数

public List<SelectListItem> Fill_Colors()
{
    List<SelectListItem> Query = new List<SelectListItem>
    {
        new SelectListItem() { Value = "Black", Text = "blk" },
        new SelectListItem() { Value = "red", Text = "red" },
        new SelectListItem() { Value = "blue", Text = "blue" }
    };

    return Query;
}
英文:

How to get multi selected values from a Selectlist?

Here is what I tried so far: following code below gets only gets the one value so issue might be with line Colors = Request.Query[&quot;Colors&quot;];

&lt;form asp-page=&quot;./index&quot; method=&quot;get&quot;&gt;
     &lt;select asp-for=&quot;Colors&quot; asp-items=&quot;@Model.Colors_SELECT&quot; class=&quot;MultiSelect&quot; multiple&gt;
        &lt;option value=&quot;&quot;&gt;All&lt;/option&gt;
    &lt;/select&gt;
&lt;/form&gt;

url example:
https://localhost:7221/Index?Colors=&Blk&Colors=red&Colors=blue

back-end code

   [BindProperty(SupportsGet = true)]
    public string? Colors { get; set; }
    public SelectList? Colors_SELECT { get; set; }

    public async Task OnGetAsync()
    {
           // Fill select values
           Colors_SELECT = new SelectList(_services.Fill_Colors(), &quot;Value&quot;, &quot;Text&quot;, 1);
      
        // selected values 
        Colors = Request.Query[&quot;Colors&quot;];
    }

helper function to fill colors

    public List&lt;SelectListItem&gt; Fill_Colors()
    {
        List&lt;SelectListItem&gt; Query = new List&lt;SelectListItem&gt;
        {
            new SelectListItem() { Value = &quot;Black&quot;, Text = &quot;blk&quot; },
            new SelectListItem() { Value = &quot;red&quot;, Text = &quot;red&quot; },
            new SelectListItem() { Value = &quot;blue&quot;, Text = &quot;blue&quot; }
        };

        return Query;
    }

答案1

得分: 1

你需要将Colors属性的类型更改为字符串数组:

public string[]? Colors { get; set; }

然后,在调用OnGetAsync方法时,Colors数组将包含查询字符串中的值。

[BindProperty(SupportsGet = true)]
public string[]? Colors { get; set; }
public SelectList? Colors_SELECT { get; set; }

public async Task OnGetAsync()
{
    // 填充选择值
    Colors_SELECT = new SelectList(_services.Fill_Colors(), "Value", "Text", 1);
  
    // 选定的值
    foreach(var color in Colors)
    {
        Console.WriteLine(color);
    }
    // https://localhost:7221/Index?Colors=black&Colors=red&Colors=blue
    // 将打印: "black", "red", "blue"
}
英文:

You need to change the type of the Colors property to string array:

public string[]? Colors { get; set; }

Then when OnGetAsync is called the Colors array will contain the values from the query string.

[BindProperty(SupportsGet = true)]
public string[]? Colors { get; set; }
public SelectList? Colors_SELECT { get; set; }

public async Task OnGetAsync()
{
    // Fill select values
    Colors_SELECT = new SelectList(_services.Fill_Colors(), &quot;Value&quot;, &quot;Text&quot;, 1);
  
    // selected values
    foreach(var color in Colors)
    {
        Console.WriteLine(color);
    }
    // https://localhost:7221/Index?Colors=black&amp;Colors=red&amp;Colors=blue
    // will print: &quot;black&quot;, &quot;red&quot;, &quot;blue&quot;
}

</details>



huangapple
  • 本文由 发表于 2023年1月9日 05:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051323.html
匿名

发表评论

匿名网友

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

确定