已添加具有相同键的项目。

时间:2011-08-15 20:43:08

标签: asp.net-mvc-2

我正在一个网站上工作,我正在尝试移动简单的变量,比如字符串和Ints来回,影响不大。话虽这么说,我在TempData和ViewData Dictionaries中设置了一些变量。然后我导航到适当的视图,完成后我回到原始视图,我得到了变量。然后我尝试导航回到视图,突然间我得到了这个错误......

    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 个答案:

答案 0 :(得分: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"));
}

和模板相同