Reputation: 9737
I am working on a website and I am trying to move simple variables, like strings and Ints back and forth with little impact. That being said, I set some variables in both TempData, and ViewData Dictionaries. I then navigate to the appropriate view, When done with that I go back to the original view where I got the variables. Then I try and navigate back to the view, and all of sudden I get this error...
An item with the same key has already been added.
I am absolutely dumbfounded. I have some if statements to check if the key is in the dictionary. What the heck am I doing wrong?
[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");
}
Above is the code for the view... What on earth am I doing wrong?
Upvotes: 0
Views: 2459
Reputation: 9668
You have to do some logging to know what exact values are in Data before error
It could be somewhere here
if ((TempData.ContainsValue(PopulationID) == false) ||
(TempData.ContainsKey("POPULATIONID") == false))
Now, if TempDate contains that key but value is null it throws exception on trying to add key
Try something like this to get more info
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"));
}
and the same for templates
Upvotes: 1