Home > Mobile >  Call two actions from Html.BeginForm ASP.NET MVC
Call two actions from Html.BeginForm ASP.NET MVC

Time:06-29

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()" />

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?

CodePudding user response:

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;
});

CodePudding user response:

@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" }))

  • Related