这两天用MVC2搞了一个软件,结果今天调试时碰到一个很头疼的问题。
只要使用HtmlHelper的控件模版在界面上显示控件,如下:
就报出An item with the same key has already been added这个错误。
后来在国外网站上才查到一个老兄也碰到这个问题,原因是对应的实体中有重名的属性。
像我这里 UserInfo实体类中有一个"ID"属性,这个实体类继承自EntityBase类,基类中有一个属性"Id".
注意这里的两个属性大小写不同,C#会认为是两个属性,但是在MVC的Request.Form中Key值是不区分大小写的。
这样就会出现添加重复键值的错误。
这个错误很少发生,因为如果大小写相同它也会被认为是一个属性,只有大小写不同时才会发生。
An item with the same key has already been added.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originate