2015-02-11 60 views
-1

我有以下的代碼,我使用的是獲得從表單收集值遍歷形式收集和將數據添加到列表

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行這是布爾和TBL是一個int

任何人都可以讓我知道我錯過

--------- -----額外的代碼-------------------

public ActionResult FlowItems(FormCollection fc) 

地表溫度在foreach循環

+0

你能證明什麼'fc'樣子..聲明lst'的'實例,但在代碼是你真正使用它..你有現有的代碼不是很清楚..你可以顯示什麼價值在'fc',你正在分裂,以獲得更好的理解..?謝謝 – MethodMan 2015-02-11 16:28:32

+0

@MethodMan fc在ActionResult中,而lst在foreach循環中 – 2015-02-11 16:35:09

+0

很高興看到這個值看起來像減輕所有猜測工作的例子。 – MethodMan 2015-02-11 16:39:52

回答

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); 
} 

有一個警告這種方法。如果有數據從col1col2失蹤。例如

var col3 = new NameValueCollection 
{ 
    { "IsVisible", "True" }, 
    { "Editable", "" }, 
    // { "Revisable", "True" }, Missing this entry 
    { "tblId", "102" }, 
}; 

然後循環超出界限。

+0

嗨@Romoku感謝,這樣的作品,我現在是一名初級程序員用有點多知識:) – 2015-02-11 21:46:01

0

的問題可能是個Ëcollection包含逗號分隔值(我假設,因爲Split()一開始你的問題),但在for循環您直接使用值不會對逗號分割。所以我想這將是試圖建立一個布爾出來的值的第2個字符(1指數)。

試試這個:

 foreach (var _key in fc.Keys) 
     { 
      var _value = fc[_key.ToString()]; 
      string[] tokenized = _value.Split(','); 
      bool b = Convert.ToBoolean(tokenized[1]); 
     }