已经添加了具有相同键的项目c#MVC [英] An item with the same key has already been added c# MVC

查看:61
本文介绍了已经添加了具有相同键的项目c#MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mvc中的ajax表单上,当我点击按钮然后



错误:



1。 已经添加了具有相同密钥的项目。



2.控制台显示无法加载资源:服务器响应状态为500(内部服务器)错误)。



我试图搜索第一个错误但是还没有得到解决方案。我还试图搜索第二个错误,但我仍然没有解决方案。 />


谢谢。

解决方案

我们在这里无法帮助您:我们无法访问您的系统,您的代码或您的数据。

首先查看错误消息:

具有相同密钥的项目已经存在添加。

这是一个非常明显的问题:您的代码尝试插入一个值与现有值匹配的行,但该列被标记为唯一。



这可能有很多原因:从得到t他提出了下一个值,并假设您可以在多用户系统中使用它,在进行更新时进行INSERT操作。

我们无法分辨。首先从调试器开始,然后查看生成错误的代码。查看它正在使用的数据,并查看您的数据库。然后尝试看看它抱怨的价值,以及你为什么要创建副本。



抱歉,我们不能为你做任何事情!


Web服务器上的错误代码500表示脚本错误,并且是可以给出的最一般描述类型的东西。只是说某些页面无法显示,因为某些尝试执行失败。

您已添加相同键的项目也是一般的错误,但通常它可能尝试设置相同的键在数据库中(或忘记设置没有默认值的键,因此例如,如果是int,则所有行将为0,默认值为de)或字典。



或许尝试运行页面上的代码失败,这部分必须是显而易见的,然后如果有读取字典或数据库操作插入或任何此类,添加检查存在。



如果你部署.net代码的调试版本,那么快速添加一个错误处理程序,它会给callstack提供错误的行号。



如果您可以调试原因代码,请转到debug =>例外=>检查托管代码异常'Thrown'下的checbox,你的执行应该在导致错误的行上停止 - 或者至少是2.尝试插入或更新失败的时间。


< blockquote>除了非常好的解决方案1和2之外,您还可以在发生错误时调整aite返回的信息,以便显示详细的错误。



通常,您希望隐藏生产站点中的错误,因此上述技巧仅应临时用于未经过密集使用的站点。



否则,更好运行应用程序local'y并重现问题并使用调试器查找抛出异常的位置。



或者,您可以在文件中写入信息问题发生并查看该文件以找出它发生的原因。


on ajax form in mvc when i click the button then

Error :

1. "An item with the same key has already been added."

2. Console shows "Failed to load resource: the server responded with a status of 500 (Internal Server Error)".

I tried to search 1st error but did not get solution yet.Again i tried to search 2nd error also but still i have no solution.

Thanks.

解决方案

We can't help you here: we don't have access to your system, your code, or your data.
So start by looking at the error message:

An item with the same key has already been added.

That's a pretty obvious problem: your code is trying to insert a row with a value that matches an existing value but the column is marked as being unique.

There could be a huge number of reasons for this: from "getting the next value" in advance and assuming you can use that in a multiuser system, to doing an INSERT operation when you should be doing an UPDATE.
We can't tell. So start with the debugger, and look at the code that generates the error. Look at the data it is using, and look in your DB as well. Then try to see what value it complains about, and why you are trying to create a duplicate.

Sorry, but we can't do any of that for you!


Error code 500 on a webserver indicates a script error and is something of the most general type of description which can be given. Just says some page failed to display because something trying to execute failed.
Your item with the same key has already been added is also an utra general error, but typically it can be trying to set the same key in a database (or forgetting to set a key with no default value, hence for instance if an int, then all rows would be 0, de default) or a dictionary.

Perhaps try to run through the code on the page which fails, this part must be obivous and then if there are reads into dictionaries or database operations with inserts or anyting such, add a check for existence.

Utimately adding an error handler which will give the the callstack whould provide you with a line number of the error, if you deploy a debug version of your .net code.

ifyou can debug the code of cause go to debug => exceptions => check the checbox under managed code exceptions 'Thrown' and your execution should stop on the very line that cause the error - Or at least 2.nd time the insert is attempted or where the update fails.


In addition to solutions 1 and 2 that are quite good, you can also adjust the information returned by the aite when an error occurs so that it display a detailled error.

Usually, you want to hide error in a production site so the trick above should only be used temporarily on a site that is not intensively used.

Otherwise, better to run the application local'y and reproduce the problem and use a debugger to find where the exception is throw .

Alternatively, you can write information in a file when the problem occurs and look into that file to find the reason it occurs.


这篇关于已经添加了具有相同键的项目c#MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆