umair mohsin

umair mohsin

  • 1k
  • 351
  • 56.2k

append link in ajax and post id by them.

Mar 18 2024 9:00 PM
<div class="text-success">
    <p id="ac-tit">@rec.DegreeTitle</p>
    <p id="ac-ins">@rec.Institute</p>
    <p id="ac-cy">@rec.CompletionYear</p>
    <p id="ac-city">@rec.City</p>

(anchors onclick="dataEdu(@rec.AcId)" )       

(anchors  onclick="delEdu(@rec.AcId)" )                                

</div>

as this @ sign shows this data is a bunch of list coming from db and anchors  shows fetchin data by id.

what my logic is i  insert data using ajax request and display it again with same styling using append method in success of ajax request like this.

$("#addBtn").click(() => {
let formdata = $("#formacad").serialize();

$.ajax({
    type: "POST",
    url: "/Js/AddUpdateMethod",
    data: formdata,

    success: function (result) {
        let str = JSON.parse(result);
        $("#acadrec").empty();
        for (let i = 0; i < str.length; i++) {
            $("#acadrec").append(
                '<div class=text-success>' + 
                '<p>' + str[i].DegreeTitle + '</p>' +
                '<p>' + str[i].Institute + '</p>' +
                '<p>' + str[i].CompletionYear + '</p>' +
                '<p>' + str[i].City + '</p>' +
                 '(anchors  onclick="dataEdu(@rec.AcId)" ')       some issues happens with anchors that is why i put anchors like this these are normal anchors
                  '(anchors  onclick="delEdu(@rec.AcId)" ) '       some issues happens with anchors that is why i put anchors like this  these are normal anchors
                + '</div>' 
                );
                $("#panacad").slideUp()
                $("#acadrec").slideDown();
            }
            $("#panacad input[type=text]").each(function () {
                this.value = "";
            });
        }
    });
});

data posted successfully, recent record with all previous records showing in the view successfully even anchors are displaying but problem is i cant get data by using these anchors as  these anchors are append to the body using jquery and previously these anchors are binded with strongly typed view so getting data by id is very easy.

i hope i have cleared all the things regarding my issues to my readers.

after inserting,data is appending with view using jquery there is no other way to bind data if ajax request is our main concern

kindly help me out to resolve this issue or if you have any other way to do this operation in a clean manner so share it with me please


Answers (1)