Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Call two actions from Html.BeginForm ASP.NET MVC

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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?

>Solution :

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;
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading