skip to Main Content

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. 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

    Login or Signup to reply.
  2. 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:

    public class MyEntity
    {
        public int Id { get; set; }
    
        [Remote(action: "Validate", controller: "MyEntities", AdditionalFields = nameof(Fee))]
        public string Name { get; set; }
    
    
        [Remote(action: "Validate", controller: "MyEntities", AdditionalFields = nameof(Name))]
        public string Fee { get; set; }
    }
    

    Controller:

    public IActionResult Create()
    {
        return View();
    }
    
    
    [HttpPost]
    
    public async Task<IActionResult> Create([Bind("Id,Name,Fee")] MyEntity myEntity)
    {
        if (ModelState.IsValid)
        {
            _context.Add(myEntity);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(myEntity);
    }
    
     [AcceptVerbs("Post","Get")]
     public IActionResult Validate(MyEntity myEntity)
     {
         var duplicated = _context.MyEntity.FirstOrDefault(x=>x.Name==myEntity.Name&&x.Fee==myEntity.Fee)!=null?true:false;
         if (duplicated)
         {
             return Json("An Entity with the same Name& Fee has  already exists");
         }
         return Json(true);
     }
    

    View:

    <h1>Create</h1>
    
    <h4>MyEntity</h4>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form asp-action="Create">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="Name" class="control-label"></label>
                    <input asp-for="Name" class="form-control" />
                    <span asp-validation-for="Name" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Fee" class="control-label"></label>
                    <input asp-for="Fee" class="form-control" />
                    <span asp-validation-for="Fee" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
    
    <div>
        <a asp-action="Index">Back to List</a>
    </div>
    
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
    

    Result:

    enter image description here

    Login or Signup to reply.
  3. 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.

    // Sample projects array
    const projects = [
      { coursename: 'Web Development', fee: 200 },
      { coursename: 'Data Science', fee: 250 },
      { coursename: 'Mobile App Development', fee: 180 },
      { coursename: 'Graphic Design', fee: 150 },
    ];
    
    // Function to check for duplicate entry
    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
    }
    
    // Example usage
    const duplicateCheck1 = isDuplicateEntry('Data Science', 250);
    const duplicateCheck2 = isDuplicateEntry('Web Development', 150);
    
    console.log('Duplicate check for "Data Science" with fee 250:', duplicateCheck1);
    console.log('Duplicate check for "Web Development" with fee 150:', duplicateCheck2);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search