Not getting data in edit form

Viewed 41

I'm using jQuery to get data from api but the data is not displaying in edit form.

edit function:

function Edit(id) {

$.getJSON("https://localhost:7018/api/Students/GETBYID?id=" + id, function (student) {    
         $("#ed-id").val(student.id),
         $("#ed-name").val(student.name)
         $("#ed-department").val(student.department),
       
}),
window.location.replace("https://localhost:7196/Student/Edit")

}

edit button

 "<button class ='btn-success' class='btn btn-primary' onclick='Edit(" + student[i]["id"] + ")'> Edit </button>"

I want to get values from my file using webapi and then use them is edit/update form but on clicking button only the blank form is opening.

1 Answers

You need to reverse your logic: navigate to the edit page of the student. Then check the ID from the URL. If there is an ID, get the data and fill in the form.

Change your button to a link which navigates to the edit page with the corresponding student id.

<a href="https://localhost:7196/Student/Edit?id=1234567">Edit</a>

Then in the Edit page, read out the URL and look for an id key.

(function() {
  const search = window.location.search;
  const params = new URLSearchParams(search);
  const id = params.get('id');

  if (id === null) {
    // No id found, stop the function.
    return;
  }

  $.getJSON(`https://localhost:7018/api/Students/GETBYID?id=${id}`, function (student) {    
    $("#ed-id").val(student.id);
    $("#ed-name").val(student.name);
    $("#ed-department").val(student.department);
  });
}());
Related