2011-08-15 129 views
0

我在一個網站上工作,我試圖移動簡單的變量,像字符串和Ints來回影響很小。話雖如此,我在TempData和ViewData詞典中都設置了一些變量。然後,我導航到適當的視圖,完成後,我回到原始視圖中獲取變量。然後我嘗試導航回到視圖,突然間我得到這個錯誤...已添加具有相同密鑰的項目。

An item with the same key has already been added. 

我絕對傻眼了。我有一些if語句來檢查密鑰是否在字典中。我做錯了什麼?

[OutputCache(CacheProfile = "ZeroCacheProfile")] 
    public ActionResult TemplateInfo(string PopulationID) 
    { 
     client.ChannelFactory.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation; 
     string msg = (TempData.ContainsKey("message") ? TempData["message"].ToString() : ""); 
     TempData["message"] = msg; 

     //XSLTACOData template = repo.getPatAcoDat(int.Parse(PopulationPatientID)); 
     //GetPatientTemplateStr("unimportant"); 
     //List<XSLTACOData> templates = repo.SelectListACOData(int.Parse(PopulationPatientID)); 

     //XmlDocument Templates = repo.SelectTemplateInfoXML(int.Parse(PopulationPatientID)); 
     //Templates.Load(""); 
     //ACOServiceReference.ACOServiceClient client = new ACOServiceReference.ACOServiceClient(); 

     //ACOServiceRefrence.searchPopulationbyOwnerResponse resp = client.GetOwnedPopulations(); 

     //string xmlString = client.GetACOData("122");//.GetPopulationPatient("121"); 
     string templates = ""; 
     try 
     { 
      templates = client.GetACOData(PopulationID); 
      if (templates == null) 
      { 
       string site = "PopInfoErrSite"; 
       site = "PopInfoErrSite"; 
       View("PopInfoErrSite", site); 
      } 
     } 
     catch (Exception ex) 
     { 
      string errorStr = ex.InnerException.Message; 
      View("PopInfoErrSite", errorStr); 
     } 
     int PopulationPatID = Int32.Parse(PopulationID); 
     int Populationid = Int32.Parse(PopulationID); 

     if ((ViewData["TEMPLATES"] == null) || (ViewData.ContainsKey("TEMPLATES")==false)) 
     { 
      ViewData.Add("TEMPLATES", templates); 
     } 
     if ((TempData.ContainsValue(PopulationID) == false) || (TempData.ContainsKey("POPULATIONID") == false)) 
     { 
      TempData.Add("POPULATIONID", Populationid); 
     }    
     //string nullStrToCheckViewDataValue = "I am putting this string here so I can check the value of ViewData[\"TEMPLATES\"] before the view is returned. Good day sir"; 
     //nullStrToCheckViewDataValue. 
     return View("TemplateInfo"); 
    } 

上面是視圖的代碼...究竟是我在做什麼錯了?

回答

1

你必須做一些記錄知道確切值是數據錯誤

之前,這可能是這裏的某個地方現在

if ((TempData.ContainsValue(PopulationID) == false) || 
(TempData.ContainsKey("POPULATIONID") == false)) 

,如果TempDate包含關鍵,但值爲null,則拋出異常在嘗試添加這樣的關鍵

嘗試一些,以獲得更多信息

try 
{ 
    if ((TempData.ContainsValue(PopulationID) == false) || (TempData.ContainsKey("POPULATIONID") == false)) 
    { 
     TempData.Add("POPULATIONID", Populationid); 
    } 

} 
catch (Exception ex) 
{ 

    throw new Exception(ex.Message + " containsValue=" + TempData.ContainsValue(PopulationID) 
     + " containsKey=" + TempData.ContainsKey("POPULATIONID")); 
} 

和模板相同

相關問題