已添加了具有相同键的项。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentException: 已添加了具有相同键的项。
源错误:
执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
堆栈跟踪:
[ArgumentException: 已添加了具有相同键的项。]
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +9383019
System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) +252
System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer) +91
System.Web.Mvc.ModelBindingContext.get_PropertyMetadata() +228
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +392
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +147
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +98
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +2501
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +548
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +474
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +181
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830
System.Web.Mvc.Controller.ExecuteCore() +136
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +111
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +65
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +42
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +141
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +52
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8836977
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1
aspx代码:
<h2>CreateRole</h2>
<%Html.BeginForm("CreateRole", "System", FormMethod.Post, new { enctype = "multipart/form-data" }); %>
<fieldset>
<table>
<tr>
<td align="right" width="10%">
<label for="roleID">roleID:</label>
</td>
<td>
<%= Html.TextBox("roleID")%>
</td>
</tr>
<tr>
<td align="right" width="10%">
<label for="companyID">companyID:</label>
</td>
<td>
<%= Html.TextBox("companyID")%>
</td>
</tr>
<tr>
<td align="right" width="10%">
<label for="roleCategoryID">roleCategoryID:</label>
</td>
<td>
<%= Html.TextBox("roleCategoryID")%>
</td>
</tr>
<tr>
<td align="right" width="10%">
<label for="roleCNm">roleCNm:</label>
</td>
<td>
<%= Html.TextBox("roleCNm")%>
</td>
</tr>
<tr>
<td align="right" width="10%">
<label for="roleENm">roleENm:</label>
</td>
<td>
<%= Html.TextBox("roleENm")%>
</td>
</tr>
<tr>
<td align="right" width="10%">
<label for="roleDescription">roleDescription:</label>
</td>
<td>
<%= Html.TextBox("roleDescription")%>
</td>
</tr>
<tr>
<td align="right" width="10%">
<label for="uUser">uUser:</label>
</td>
<td>
<%= Html.TextBox("uUser")%>
</td>
</tr>
<tr>
<td align="right" width="10%">
<label for="uDate">uDate:</label>
</td>
<td>
<%= Html.TextBox("uDate")%>
</td>
</tr>
</table>
<div>
<input type="submit" value="确认" />
<div style="float:right;">
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</div>
</fieldset>
<%Html.EndForm(); %>
Controllers代码:
[NoCache]
public ActionResult CreateRole()
{
Role role = new Role()
{
uDate = DateTime.Now.AddDays(7)
};
return View("CreateRole",role);
}
[HttpPost]
[NoCache]
public ActionResult CreateRole(Role role)
{
if (Session["UserName"] != null)
{
DB.Role.InsertOnSubmit(role);
DB.SubmitChanges();
return RedirectToAction("roleList");
}
else
{
return RedirectToAction("LogOn", "Account");
}
}
每次点“确认”按钮时就会出现上面错误,那位高手帮帮忙,这个到底错在哪?非常感谢!
向键值对集合添加项时,判断是否已经存在了,如果没有则添加,存在则做修改等操作。