Garegin Hambardzumyan Garegin Hambardzumyan - 2 months ago 8
Ajax Question

Image value was null when posted to controller

in controller image comes null i tried this code but it did not work

function bindForm(dialog) {

$("form", dialog).submit(function () {
var formdata = new FormData($('form').get(0));
$.ajax({
url: "/Books/Create",
type: "POST",
dataType: "JSON",
data: formData,
contentType: false,
processData: false,
success: function (result) {
if (result.success) {
$("#myModal").modal("hide");
location.reload();
} else {
$("#myModalContent").html(result);
bindForm();
}
}
});
return false;
});
}

Answer

$(function () {

    $.ajaxSetup({ cache: false });

    $("a[data-modal]").on("click", function (e) {
        $("#myModalContent").load(this.href, function () {
            

            $("#myModal").modal({
                keyboard: true
            }, "show");

            bindForm(this);
        });

        return false;
    });
});

function bindForm(dialog) {
    
    $("#crtForm", dialog).submit(function () {
        var myform = document.getElementById("crtForm");
        var fdata = new FormData(myform);
       $.ajax({
            url: this.action,
            data: fdata,
            cache: false,
            processData: false,
            contentType: false,
            type: "POST",
            success: function (result) {
                if (result.success) {
                    $("#myModal").modal("hide");
                    location.reload();
                } else {
                    $("#myModalContent").html(result);
                    bindForm();
                }
            }
        });
        return false;
    });
}

Comments