i want to check if there’s duplicate entry,it will prompt message "duplicate entry" but my code doesnt do the checking for duplicate.
Index.cshtml
function addProject() {
var coursename = $('#name').val();
var fee = $('#fee').val();
// Check for duplicate entry
if (isDuplicateEntry(coursename, fee)) {
alert("Duplicate entry found. Please enter a unique course name and fee.");
return;
}
$.ajax({
type: 'POST',
url: url,
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data),
success: function (data) {
getall();
if (isNew) {
alert("Data has been added");
} else {
alert("Data has been updated");
}
}
});
}
function isDuplicateEntry(coursename, fee) {
for (var i = 0; i < projects.length; i++) {
if (projects[i].coursename === coursename && projects[i].fee === fee) {
return true; // Duplicate entry found
}
}
return false; // No duplicate entry found
}
tried to put duplicate code before function addProject()
but it doesnt change anything.
3
Answers
1.It could be cache issue. Clear cache in the server and try again
2.=== is case sensitive, verify the existing data and newly added data both are same
3.Try to debug in the developer option console
4.Or try with the alert statements for coursename variable and fee variable in the line# 2 and 3.
5.Could be decimal places in the fee might causing the issue. Try using alert or try console.log
It seems you load data from backend and check if your current entity is duplicated with js codes?(I can’t see all your codes,if not ,please point it out)
We usually check if an entity is duplicated on server to avoid loading unecessary data and it would perform better when multipule users add entity at the same time.
In asp.net core MVC project,you could try remote validation follow this document
A minimal example:
Model:
Controller:
View:
Result:
Without knowing the contents of your
projects
, it’s challenging to identify the issue. Please refer to the example below, which might assist you in resolving the problem.