[英]Alternative to Dictionary
Getting an error: on line CurrentFilters.Add($"Colors", Colors[i].ToString());
出现错误:在线CurrentFilters.Add($"Colors", Colors[i].ToString());
An unhandled exception occurred while processing the request.
ArgumentException: An item with the same key has already been added. Key: Colors
Issue: If Dictionary
can't have same key than what are my alternatives?问题:如果Dictionary
不能有与我的替代方案相同的密钥? I found few on Google like putting into a List
but they don't work with asp-all-route-data
.我在 Google 上发现很少有人喜欢放入List
,但它们不适用于asp-all-route-data
。
Also since, it is auto Bind;也因为,它是自动绑定; this mean I can't do something like CurrentFilters.Add($"Colors[i]", Colors[i].ToString());
这意味着我不能做像CurrentFilters.Add($"Colors[i]", Colors[i].ToString());
这样的事情。 this will work but it wont bind bc name "Colors[1]"
is not same as "Colors"
.这会起作用,但它不会绑定 bc name "Colors[1]"
is not same as "Colors"
。
If you click on <a>
tag than it will send URL format: localhost234/Index?SearchString=text%Colors=red&Colors=blue&SortOrder=Colors_asc
如果你点击<a>
标签,它会发送 URL 格式: localhost234/Index?SearchString=text%Colors=red&Colors=blue&SortOrder=Colors_asc
back-end code后端代码
[BindProperty(SupportsGet = true)]
public Dictionary<string, string> CurrentFilters { get; set; }
[BindProperty(SupportsGet = true)]
public List<string>? Colors { get; set; } //list of colors user selects
public SelectList? Colors_SELECT { get; set; } //fill dropdownlist with colors values
public async Task OnGetAsync()
{
Colors_SELECT = getColors(); //fill dropdown with all colors
//this fill get back into asp-all-route-data
CurrentFilters.Clear();
CurrentFilters.Add("SearchString", SearchString);
for (int i = 0; i < Colors.Count; i++)
{
// ISSUE is here
CurrentFilters.Add($"Colors", Colors[i]);
}
}
Front-End - form has many different filters for my grid-table前端 - 表单对我的网格表有许多不同的过滤器
<form asp-page="./index" method="get">
<input type="text" asp-for="SearchString" class="form-control" placeholder="Search..." />
<select asp-for="Colors" asp-items="@Model.Colors_SELECT" class="MultiSelect" multiple>...</select>
...
</form>
grid-table - display data from database. grid-table - 显示数据库中的数据。 Header links are for filtering and sorting. Header 链接用于筛选和排序。 asp-all-route-data
has different filters inside that get pass to URL asp-all-route-data
内部有不同的过滤器,可以传递给 URL
<Table>
... // header hyper link
<a asp-page="./Index" method="get"
asp-all-route-data="@Model.CurrentFilters"
asp-route-SortOrder="@Model.Colors_Sort" >
@Html.DisplayNameFor(model => model.MyList[0].Colors)
</a>
...
Update: I tried using list inside Dictionary but it gives an error.更新:我尝试在 Dictionary 中使用列表,但它给出了一个错误。
[BindProperty(SupportsGet = true)]
public Dictionary<string, List<string>> CurrentFilters { get; set; }
Error:错误:
Error: 'System.Collections.Generic.Dictionary<string,
System.Collections.Generic.List<string>>' to
'System.Collections.Generic.IDictionary<string, string>'.
An explicit conversion exists (are you missing a cast?)
Try using尝试使用
[BindProperty(SupportsGet = true)]
public Dictionary<string, List<string>> CurrentFilters { get; set; }
instead of public Dictionary<string, string> CurrentFilters { get; set; }
而不是public Dictionary<string, string> CurrentFilters { get; set; }
public Dictionary<string, string> CurrentFilters { get; set; }
To handle this in your OnGetAsync
handler do something like this:要在您的OnGetAsync
处理程序中处理此问题,请执行以下操作:
public async Task OnGetAsync()
{
CurrentFilters.Clear();
CurrentFilters.Add("SearchString", new List<string>());
CurrentFilters["SearchString"].Add(SearchString);
CurrentFilters.Add($"Colors", new List<string>());
for (int i = 0; i < Colors.Count; i++)
{
CurrentFilters[$"Colors"].Add(Colors[i].ToString());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.