遍历表单集合并将数据添加到列表
loop over forms collection and add data to list
我有以下代码,我用它从表单集合中获取值
List<FlowSettings> lst = new List<FlowSettings>();
string[] IsVisible = fc["IsVisible"].Split(',');
string[] Editable = fc["Editable"].Split(',');
string[] Revisable = fc["Revisable"].Split(',');
string[] tbl = fc["tblId"].Split(',');
以上数组仅供我使用,以确保我按预期获取数据。我的问题是,我可以遍历表单集合,但无法取出值并添加到我的列表中。
foreach (var _key in fc.Keys)
{
var _value = fc[_key.ToString()];
//lst.Add(new FlowSettings { TblId = Convert.ToInt32(_value[0]), ChxIsVisible =
Convert.ToBoolean(_value[1]),
ChxEditable = true,
ChxRevisable = true
});
}
IsVisible 等中的值有 10 行,它们是 bool,而 tbl 是一个 int
任何人都可以让我知道我错过了什么
----------------额外代码--------------------
public ActionResult FlowItems(FormCollection fc)
lst 在 foreach 循环中
问题可能是该集合包含以逗号分隔的值(我假设是因为 Split()
在您的问题开头)但在 for 循环中您直接使用该值不用逗号分隔。所以我猜它会尝试从值的第二个字符(索引 1)中创建一个布尔值。
试试这个:
foreach (var _key in fc.Keys)
{
var _value = fc[_key.ToString()];
string[] tokenized = _value.Split(',');
bool b = Convert.ToBoolean(tokenized[1]);
}
FormCollection
没有实现IDictionary(TKey,TValue)
接口,所以需要循环获取值。
数据
public class FlowSettings
{
public bool IsVisible { get; set; }
public bool Editable { get; set; }
public bool Revisable { get; set; }
public int TblId { get; set; }
}
private bool ParseBool(string value)
{
return Convert.ToBoolean(EmptyToFalse(value));
}
private int ParseInt(string value)
{
return Convert.ToInt32(EmptyToInvalid(value));
}
private string EmptyToFalse(string value)
{
return string.IsNullOrWhiteSpace(value) ? bool.FalseString : value;
}
private string EmptyToInvalid(string value)
{
return string.IsNullOrWhiteSpace(value) ? "-1" : value;
}
创建
var col1 = new NameValueCollection
{
{ "IsVisible", "True" },
{ "Editable", "True" },
{ "Revisable", "True" },
{ "tblId", "100" },
};
var col2 = new NameValueCollection
{
{ "IsVisible", "True" },
{ "Editable", "" },
{ "Revisable", "True" },
{ "tblId", "101" },
};
var formCollection = new FormCollection
{
col1,
col2
};
var length =
formCollection
.Cast<string>()
.Select(entry => formCollection.GetValues(entry).Length)
.Max();
循环
var items = new List<FlowSettings>();
for(var i = 0; i < length; i++)
{
var flowSettings = new FlowSettings
{
IsVisible = ParseBool(formCollection.GetValues("IsVisible")[i]),
Editable = ParseBool(formCollection.GetValues("Editable")[i]),
Revisable = ParseBool(formCollection.GetValues("Revisable")[i]),
TblId = ParseInt(formCollection.GetValues("tblId")[i]),
};
items.Add(flowSettings);
}
这种方法有一个警告。如果 col1
或 col2
中缺少数据。例如
var col3 = new NameValueCollection
{
{ "IsVisible", "True" },
{ "Editable", "" },
// { "Revisable", "True" }, Missing this entry
{ "tblId", "102" },
};
然后是越界循环
我有以下代码,我用它从表单集合中获取值
List<FlowSettings> lst = new List<FlowSettings>();
string[] IsVisible = fc["IsVisible"].Split(',');
string[] Editable = fc["Editable"].Split(',');
string[] Revisable = fc["Revisable"].Split(',');
string[] tbl = fc["tblId"].Split(',');
以上数组仅供我使用,以确保我按预期获取数据。我的问题是,我可以遍历表单集合,但无法取出值并添加到我的列表中。
foreach (var _key in fc.Keys)
{
var _value = fc[_key.ToString()];
//lst.Add(new FlowSettings { TblId = Convert.ToInt32(_value[0]), ChxIsVisible =
Convert.ToBoolean(_value[1]),
ChxEditable = true,
ChxRevisable = true
});
}
IsVisible 等中的值有 10 行,它们是 bool,而 tbl 是一个 int
任何人都可以让我知道我错过了什么
----------------额外代码--------------------
public ActionResult FlowItems(FormCollection fc)
lst 在 foreach 循环中
问题可能是该集合包含以逗号分隔的值(我假设是因为 Split()
在您的问题开头)但在 for 循环中您直接使用该值不用逗号分隔。所以我猜它会尝试从值的第二个字符(索引 1)中创建一个布尔值。
试试这个:
foreach (var _key in fc.Keys)
{
var _value = fc[_key.ToString()];
string[] tokenized = _value.Split(',');
bool b = Convert.ToBoolean(tokenized[1]);
}
FormCollection
没有实现IDictionary(TKey,TValue)
接口,所以需要循环获取值。
数据
public class FlowSettings
{
public bool IsVisible { get; set; }
public bool Editable { get; set; }
public bool Revisable { get; set; }
public int TblId { get; set; }
}
private bool ParseBool(string value)
{
return Convert.ToBoolean(EmptyToFalse(value));
}
private int ParseInt(string value)
{
return Convert.ToInt32(EmptyToInvalid(value));
}
private string EmptyToFalse(string value)
{
return string.IsNullOrWhiteSpace(value) ? bool.FalseString : value;
}
private string EmptyToInvalid(string value)
{
return string.IsNullOrWhiteSpace(value) ? "-1" : value;
}
创建
var col1 = new NameValueCollection
{
{ "IsVisible", "True" },
{ "Editable", "True" },
{ "Revisable", "True" },
{ "tblId", "100" },
};
var col2 = new NameValueCollection
{
{ "IsVisible", "True" },
{ "Editable", "" },
{ "Revisable", "True" },
{ "tblId", "101" },
};
var formCollection = new FormCollection
{
col1,
col2
};
var length =
formCollection
.Cast<string>()
.Select(entry => formCollection.GetValues(entry).Length)
.Max();
循环
var items = new List<FlowSettings>();
for(var i = 0; i < length; i++)
{
var flowSettings = new FlowSettings
{
IsVisible = ParseBool(formCollection.GetValues("IsVisible")[i]),
Editable = ParseBool(formCollection.GetValues("Editable")[i]),
Revisable = ParseBool(formCollection.GetValues("Revisable")[i]),
TblId = ParseInt(formCollection.GetValues("tblId")[i]),
};
items.Add(flowSettings);
}
这种方法有一个警告。如果 col1
或 col2
中缺少数据。例如
var col3 = new NameValueCollection
{
{ "IsVisible", "True" },
{ "Editable", "" },
// { "Revisable", "True" }, Missing this entry
{ "tblId", "102" },
};
然后是越界循环