我正在尝试从记录列表中删除一条记录。之前工作正常。现在,当我在ajax函数中按Delete时,它将引发异常“已添加具有相同键的项。”
我已经尝试过从有关此主题的已回答问题中选择一些方法,但是我无法解决我的问题。我无法找出我的代码中重复的名称或其他重复的实体/变量的位置。
HTML
<td>
<div class="hidden-phone visible-desktop btn-group">
<a href="@Url.Content("~/backend/Car/UpdateUsedCar/")@CryptoProvider.Encrypt(Car.Id)" class="btn btn-mini btn-info" title="Edit Car"><i class="icon-edit"></i></a>
@*<a href="@Url.Content("~/backend/Car/UsedCarDetails/")@CryptoProvider.Encrypt(Car.Id)" class="btn btn-mini btn-info" title="Details Car"><i class="icon-play"></i></a>*@
<a href="@Url.Content("~/backend/Car/DeleteUsed/")@CryptoProvider.Encrypt(Car.Id)" onclick='javascript:return deleteConfirmation(this);' class='btn btn-mini btn-danger' title="Delete Car"><i class="icon-trash"></i></a>
<a class="invisible" href="@Url.Content("~/backend/Car/DisableCar/")@CryptoProvider.Encrypt(Car.Id)" onclick='javascript:return deleteConfirmation(this);' class='btn btn-mini btn-danger' title="Restore Car"><i class="icon-ok"></i></a>
</div>
<div class="hidden-desktop visible-phone">
<div class="inline position-relative">
<button class="btn btn-minier btn-yellow dropdown-toggle" data-toggle="dropdown"><i class="icon-caret-down icon-only"></i></button>
<ul class="dropdown-menu dropdown-icon-only dropdown-yellow pull-right dropdown-caret dropdown-close">
<li><a href="@Url.Content("~/backend/Car/UpdateUsedCar/")@CryptoProvider.Encrypt(Car.Id)" class="tooltip-success" data-rel="tooltip" title="Edit Car" data-placement="left"><span class="green"><i class="icon-edit"></i></span></a></li>
@*<li><a href="@Url.Content("~/backend/Car/UsedCarDetails/")@CryptoProvider.Encrypt(Car.Id)" class="tooltip-success" data-rel="tooltip" title="Details Car" data-placement="left"><span class="green"><i class="icon-play"></i></span></a></li>*@
<li><a href="@Url.Content("~/backend/Car/DeleteUsed/")@CryptoProvider.Encrypt(Car.Id)" onclick='javascript:return deleteConfirmation(this);' class='tooltip-error' data-rel="tooltip" title="Delete Car" data-placement="left"><span class="red"><i class="icon-trash"></i></span></a></li>
<li class="invisible"><a href="@Url.Content("~/backend/Car/DisableCar/")@CryptoProvider.Encrypt(Car.Id)" onclick='javascript:return deleteConfirmation(this);' class='tooltip-error' data-rel="tooltip" title="Restore Car" data-placement="left"><span class="red"><i class="icon-ok"></i></span></a></li>
</ul>
</div>
</div>
</td>
jQuery函数以确认删除
function deleteConfirmation(link) {
debugger;
var index = $(link).parent().parent().parent().index();
$('.deleteThisRecord').removeClass('deleteThisRecord'); // Unselect All
$(link).addClass('deleteThisRecord');
var message = GetMessageBox(confirmHeading, deleteMessage, true);
$('#dataConfirmModal').remove();
$('body').append(message);
//if (!$('#dataConfirmModal').length) {
// $('body').append(message);
//}
$('#dataConfirmModal').find('.modal-body').text($(link).attr('data-confirm'));
$('#dataConfirmOK').attr('onclick', 'javascript:ajaxDeletion(' + index + ');');
$('#dataConfirmModal').modal({ show: true });
return false;
}
function ajaxDeletion(pos) {
debugger;
//var url = $('tbody').children('tr').eq(pos).find('.icon-trash').parent().attr('href');
//var ico = $('tbody').children('tr').eq(pos).find('.icon-trash');
//var tr = $('tbody').children('tr').eq(pos);
//var row = $(ico).closest('tr');
var url = $('.deleteThisRecord').attr('href');
var row = $('.deleteThisRecord').parents('tr').eq(0);
var nRow = row[0];
$.ajax({
url: url,
type: "POST",
dataType: "json",
success: function (data, status) {
debugger;
if (data.status == 'success') {
//oTable.fnDeleteRow(pos);
//var table1 = $('tbody').parent();
//alert(tabl1);
//table1.deleteRow(pos);
//while (table && table.tagName != 'TABLE')
// table = table.parentNode;
//if (!table)
// return;
//table.deleteRow(row.rowIndex);
oTable.fnDeleteRow(nRow);
$('#dataConfirmLabel').html(successHeading);
$('.modal-body').html(data.message);
VerifyTable();
} else {
debugger;
$('#dataConfirmLabel').html(failedHeading);
$('.modal-body').html(data.message);
}
debugger;
$('#btnDialogCancel').hide();
$('#dataConfirmOK').attr('onclick', '');
$('#dataConfirmOK').attr('onclick', "$('.close').click();");
},
error: function (data, status) {
debugger;
//console.log(data);
//alert(" Can't do because: " + error);
}
});
return false;
}
控制器中的功能
public ActionResult DeleteUsed(KapCarViewModel model)
{
delete record
}
用于从前端解密加密值的功能
public bool ContainsPrefix(string prefix) {
try {
if (_routeData.Values[prefix] == null || !prefix.ToLower().Equals("id") || _id > 0 || Convert.ToString(_routeData.Values["controller"]) == "Home" ) {
return false;
}
_routeData.Values[prefix] = CryptoProvider.Decrypt(_routeData.Values[prefix].ToString());
_id = Convert.ToInt32(_routeData.Values[prefix]);
return true;
}
catch (Exception)
{
throw new Exception("Invalid request found.");
}
return false;
}
查看模型以获取/设置值
public class KapCarViewModel : BaseViewModel
{
public int Id { get; set; }
public string EncryptedId { get; set; }
}
我需要在用户确认对话框中的“确定”后删除记录。 但是“已经添加了具有相同键的项目。”此错误不允许我这样做。我没有从头开始编写此代码,但以前运行良好。突然,它停止工作了。请指导我解决这个问题。 注意:当此代码从前端解密记录ID值时。解密函数然后访问引发错误的视图模型