![]()
Şimdide Ajax ile Post işlemi ile Update nasıl yapılır sırasi ile ona bakalım.
Önce BackEnd tarafını yazdım
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[HttpPost] public IActionResult UpdateStudent(Student student) { var studentM = students.FirstOrDefault(i => i.Id == student.Id); if (studentM != null) { studentM.Name = student.Name; studentM.Gender = student.Gender; studentM.Age = student.Age; var studentJson = JsonConvert.SerializeObject(student); return Json(studentJson); } else { return View(); } } |
Sonrada Önyüz tarafını yazdım
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<div> <button type=“button” id=“btnGetUpdate” class=“btn btn-warning mb-3”> Student Update</button><br /> <span>Id</span> <input type=“text” class=“form-control col-6” id=“txtStudentId” /><br /> <span>Name</span> <input type=“text” class=“form-control col-6” id=“txtStudentName” /><br /> <span>Gender</span> <input type=“text” class=“form-control col-6” id=“txtStudentGender” /><br /> <span>Age</span> <input type=“text” class=“form-control col-6” id=“txtStudentAge” /><br /> </div> $(“#btnGetUpdate”).click(function () { let data = { Id: $(“#txtStudentId”).val(), Name: $(“#txtStudentName”).val(), Gender: $(“#txtStudentGender”).val(), Age: $(“#txtStudentAge”).val() }; $.ajax({ type: “Post”, url: “/Home/UpdateStudent”, data: data, success: function (response) { let result = jQuery.parseJSON(response) alert(“Successfully updated”); }, error: function () { alert(“Content load failed!!”); } }); }); |
Sonuç


Sağlıcakla….








Bir yanıt yazın