![]()
Şimdide Ajax ile Post işlemi ile Ekleme 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 |
[HttpPost] public IActionResult AddStudent(Student student) { if(student != null) { students.Add(student); 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 38 |
<div> <button type=“button” id=“btnAddSingle” class=“btn btn-secondary mb-3”>Add Student</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> $(“#btnAddSingle”).click(function () { let data = { Id: $(“#txtStudentId”).val(), Name: $(“#txtStudentName”).val(), Gender: $(“#txtStudentGender”).val(), Age: $(“#txtStudentAge”).val() }; $.ajax({ type: “Post”, url: “/Home/AddStudent”, data: data, success: function (response) { let result = jQuery.parseJSON(response) alert(“Successfully added”); }, error: function () { alert(“Content load failed!!”); } }); }); |
Sonuç

sağlıcakla kalın








Bir yanıt yazın