英文:
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["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 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(), "Value", "Text", 1);
// selected values
Colors = Request.Query["Colors"];
}
helper function to fill 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;
}
答案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(), "Value", "Text", 1);
// selected values
foreach(var color in Colors)
{
Console.WriteLine(color);
}
// https://localhost:7221/Index?Colors=black&Colors=red&Colors=blue
// will print: "black", "red", "blue"
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论