【问题标题】:alternative to Dictionary (ArgumentException: An item with the same key has already been added)替代字典(ArgumentException:已添加具有相同键的项目)
【发布时间】:2023-01-11 08:34:13
【问题描述】:

出现错误:在线 CurrentFilters.Add($"Colors", Colors[i].ToString());

处理请求时发生未处理的异常。 ArgumentException:已添加具有相同键的项目。 关键:颜色

问题:如果Dictionary 不能拥有与我的替代方案相同的密钥?我在 Google 上发现很少有人喜欢输入 List,但它们不适用于 asp-all-route-data

也因为,它是自动绑定;这意味着我不能做像 CurrentFilters.Add($"Colors[i]", Colors[i].ToString()); 这样的事情,这会起作用,但它不会绑定 Colors[1]Colors 不同。

默认网址格式:localhost234/index?Colors=red&Colors=blue&Colors=Black

后端代码

[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 
    
     CurrentFilters.Clear();
     CurrentFilters.Add("SearchString", SearchString);
     for (int i = 0; i < Colors.Count; i++)
     {
          CurrentFilters.Add($"Colors", Colors[i].ToString());
     }
  }

前端表单 - 多色下拉列表

<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>

这里我在表中显示数据。标题链接用于过滤和排序。我有多个过滤器,所以我将它们添加到asp-all-route-data

<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>
...

更新:我尝试在 Dictionary 中使用列表,但它给出了一个错误。

错误(活动)CS0266 无法隐式转换类型 'System.Collections.Generic.Dictionary<字符串, System.Collections.Generic.List>' 到 'System.Collections.Generic.IDictionary<string, string>'。明确的 转换存在(你错过了演员表吗?)

[BindProperty(SupportsGet = true)]
public Dictionary<string, List<string>> CurrentFilters { get; set; }

【问题讨论】:

  • 使用Dictionary&lt;string, List&lt;string&gt;&gt;?或者使用asp-all-route-data以外的东西...
  • 你还没有提供任何关于你打算用CurrentFilters做什么的信息。您希望CurrentFilters["Colors"] 得到什么结果?它将如何使用?
  • 顺便说一句,为什么你有Colors[i].ToString()?你知道Colors类型是List&lt;string&gt;所以Colors[i]的类型已经是string。当您不插入任何值时,为什么要执行 $"Colors"
  • 我尝试使用 Dictionary&lt;string, List&lt;string&gt;&gt; 但它给出了相同的转换错误

标签: c# asp.net asp.net-mvc razor razor-pages


【解决方案1】:

尝试使用

[BindProperty(SupportsGet = true)]
public Dictionary<string, List<string>> CurrentFilters { get; set; }

而不是public Dictionary&lt;string, string&gt; CurrentFilters { get; set; }

要在您的 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());
    }
}

【讨论】:

  • 我尝试使用但它总是给我同样的错误:Dicitionary&lt;string, List&lt;string&gt;&gt; CurrentFilter cannot implicitly convert type system.collections.generic.Dictionary&lt;string, System.Collections.Generic.List&lt;string&gt;&gt; to System.collections.Generic.IDictionary&lt;string, string&gt;. An explicit conversion exists (are you missing a cast?)
猜你喜欢
  • 2013-02-16
  • 2013-01-14
  • 1970-01-01
  • 2017-05-14
  • 2013-03-18
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
相关资源
最近更新 更多