我有一个简单的网格,标题内有几个文本框,我想用它们来过滤网格。 为此,我创建了一个函数,该函数将创建或更新cookie,我需要将cookie存储/保存在某个地方。我希望过滤器在刷新页面后仍保留在那里。
因此,当用户填写文本框并按Enter时,将执行以下代码:
...
model.FilteringValue = new List<FilterValues>();
//fill cookies
foreach (var filter in columnsFilter)
{
var filtercolumn = filter.Key.ToString() + "filter";
//Checken if cookie exists
if (Request.Cookies[filtercolumn] != null)
{
//Cookie does exists. proceed
//Check if value has a value.
if(filter.Value != "")
{
//Filter has a value, update the cookie
Response.Cookies[filtercolumn].Value = filter.Value.ToString();
}
else
{
//Filter.value is empty. which means the user has removed the input textbox's value and we should now delete or empty the cookie
Response.Cookies[filtercolumn].Value = filter.Value.ToString();
}
}
else
{
//Cookie create.
//check if filter has value
if (filter.Value != "")
{
SetCookies(filter.Key + "filter", filter.Value.ToString());
}
else
{
SetCookies(filter.Key + "filter", "");
}
}
//Set cookie value to the model we use for the view.
model.ColumnsFilter[filter.Key] = Request.Cookies[filtercolumn].Value.ToString();
}
for (int i = 0; i < model.ColumnsFilter.Count; i++)
{
foreach(var item in model.ColumnsFilter)
{
//Fill list with ModelColumnsfilter and values.
//Use that model list in view to give values to the textboxes.
// model.FilteringValue[i].Key = item.Key;
// model.FilteringValue[i].Value = item.Value;
}
}
return PartialView("_ItemsToPlanGrid", model);
哪个工作还可以。一旦文本框填满,就将设置cookie,这很好。
错误出现在这里:
for (int i = 0; i < model.ColumnsFilter.Count; i++)
{
foreach(var item in model.ColumnsFilter)
{
//Fill list with ModelColumnsfilter and values.
//Use that model list in view to give values to the textboxes.
model.FilteringValue[i].Key = item.Key;
model.FilteringValue[i].Value = item.Value;
}
}
我想将字典中的项目存储在一个列表中,所以我可以用该值填充视图中的文本框。
System.ArgumentOutOfRangeException:'索引超出范围。一定是 非负数且小于集合的大小。'