skip to Main Content

So the first action would be:

@using (Html.BeginForm("Edit", "Post", FormMethod.Post, new { enctype = "multipart/form-data" }))

and the second would be a ajax call:

<input type="submit" value="Save" onclick="deleteImages()" class="btn btn-default" />

Ajax:

<script>
    let deletedImages = [];
    function Remove(id, e) {
        deletedImages.push(id);
        console.log(deletedImages);
        $(e).fadeOut(1000);
        $(e).next().fadeOut(1000);
    }

//starts here

    function deleteImages() {
    $.ajax({
        type: 'POST',
        url: "@Url.Action("DeleteImages", "Image")",
        data: { "deletedImages": deletedImages },
        success: function(data) {
            alert("deleted");
        },
        error: function(data) {
            window.location.reload();
        }
    });
    }
</script>

Only the onClick function is getting called, whereas the Post/Edit of the Html.BeginForm is not.
Any suggestions on how to fix this?

2

Answers


  1. @using (Html.BeginForm("Edit", "Post", FormMethod.Post, new { enctype = "multipart/form-data" }))

    Maybe you are missing Area?

    Something like

    @using (Html.BeginForm("Edit", "Post", new { area = "" }, FormMethod.Post, new { enctype = "multipart/form-data" }))

    Login or Signup to reply.
  2. you can use onsubmit. run onsubmit before post

    @using (Html.BeginForm("Edit", "Post", FormMethod.Post, new
    { onsubmit="deleteImages()", enctype = "multipart/form-data" }))
    

    second option: (using jquery)

    $('form').submit(function() {
       deleteImages();
       return true;
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search