Member 7969814 Ответов: 0

Как отправить нормальная модель представления виде FormData и данных JSON в AJAX-ответа?


Привет я хочу отправить свой модальный объект view и два объекта массива javascript в одном сообщении ajax

как я могу это сделать, пожалуйста, помогите мне.

это мой AJAX-ответа
$(document).ready(function() {
    $("#formID").on("submit", function(event) {
        var $this = $(this);
        var frmValues = $this.serialize();
        var myTab = document.getElementById('TB1');
        var Details2 = [];
        var Details = [];
        //LOOP THROUGH EACH ROW OF THE TABLE AFTER HEADER.
        for (i = 1; i < myTab.rows.length; i++) {
            //Details.push([myTab.rows[i].cells[1].children[0].value, myTab.rows[i].cells[2].children[0].value, myTab.rows[i].cells[3].children[0].value]);
            Details.push({ 'Qualification': myTab.rows[i].cells[0].children[0].value, 'PassingYear': myTab.rows[i].cells[1].children[0].value, 'Division': myTab.rows[i].cells[2].children[0].value, 'Percnet': myTab.rows[i].cells[3].children[0].value, 'Institute': myTab.rows[i].cells[4].children[0].value, 'Remark': myTab.rows[i].cells[5].children[0].value })
            //Details2.push({ 'CustomerName': myTab.rows[i].cells[1].children[0].value, 'ProjectName': myTab.rows[i].cells[2].children[0].value, 'ProjectStatus': myTab.rows[i].cells[3].children[0].value })
            Details2.push({ 'Qualification': myTab.rows[i].cells[0].children[0].value, 'PassingYear': myTab.rows[i].cells[1].children[0].value, 'Division': myTab.rows[i].cells[2].children[0].value, 'Percnet': myTab.rows[i].cells[3].children[0].value, 'Institute': myTab.rows[i].cells[4].children[0].value, 'Remark': myTab.rows[i].cells[5].children[0].value })

        }
        $.ajax({
            cache: false,
            async: true,
            type: "POST",
            url: "@Url.Action("Create", "EmpLoyee")",
            data: frmValues,
            data: JSON.stringify(Details),
            data: JSON.stringify(Details2),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            traditional: true,
            success: function(data) {
                alert(data.FirstName);
            }
        });
    });
});

это тег формы
@using (Html.BeginForm(null,null, FormMethod.Post, new { id = "formID" }))

это контроллер
public ActionResult Create(EmployeeDAL OBJ, List<Details> Details, List<Details> Details2)
{

}


Что я уже пробовал:

Я попробовал эти два кода
$(function () {
    $("#theButton").click(function (event) {
        event.preventDefault();
        //event.stopImmediatePropagation();
        var $this = $(this);
        var OBJ = $this.serialize();
        var myTab = document.getElementById('TB1');
        var Details2 = [];
        var Details = [];
         //LOOP THROUGH EACH ROW OF THE TABLE AFTER HEADER.
        for (i = 1; i < myTab.rows.length; i++) {
            //Details.push([myTab.rows[i].cells[1].children[0].value, myTab.rows[i].cells[2].children[0].value, myTab.rows[i].cells[3].children[0].value]);
            Details.push({ 'Qualification': myTab.rows[i].cells[0].children[0].value, 'PassingYear': myTab.rows[i].cells[1].children[0].value, 'Division': myTab.rows[i].cells[2].children[0].value, 'Percnet': myTab.rows[i].cells[3].children[0].value, 'Institute': myTab.rows[i].cells[4].children[0].value, 'Remark': myTab.rows[i].cells[5].children[0].value })
            //Details2.push({ 'CustomerName': myTab.rows[i].cells[1].children[0].value, 'ProjectName': myTab.rows[i].cells[2].children[0].value, 'ProjectStatus': myTab.rows[i].cells[3].children[0].value })
            Details2.push({ 'Qualification': myTab.rows[i].cells[0].children[0].value, 'PassingYear': myTab.rows[i].cells[1].children[0].value, 'Division': myTab.rows[i].cells[2].children[0].value, 'Percnet': myTab.rows[i].cells[3].children[0].value, 'Institute': myTab.rows[i].cells[4].children[0].value, 'Remark': myTab.rows[i].cells[5].children[0].value })

        }

        $.ajax({
            type: "POST",
            //traditional: true,
            url: "/EmpLoyee/Create",
            data: JSON.stringify(OBJ),
            data: JSON.stringify(Details),
            data: JSON.stringify(Details2),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            traditional: true,
            success: function (response) {
                alert(response.massge + " record(s) inserted.");
                $("#div").html(response);
                if (response.d == true) {
                    alert("You will now be redirected.");
                    window.location = "//www.aspsnippets.com/";
                }
            }
        });
    })
})
$(document).ready(function() {
    $("#formID").on("submit", function(event) {
        var $this = $(this);
        var frmValues = $this.serialize();
        var myTab = document.getElementById('TB1');
        var Details2 = [];
        var Details = [];
        //LOOP THROUGH EACH ROW OF THE TABLE AFTER HEADER.
        for (i = 1; i < myTab.rows.length; i++) {
            //Details.push([myTab.rows[i].cells[1].children[0].value, myTab.rows[i].cells[2].children[0].value, myTab.rows[i].cells[3].children[0].value]);
            Details.push({ 'Qualification': myTab.rows[i].cells[0].children[0].value, 'PassingYear': myTab.rows[i].cells[1].children[0].value, 'Division': myTab.rows[i].cells[2].children[0].value, 'Percnet': myTab.rows[i].cells[3].children[0].value, 'Institute': myTab.rows[i].cells[4].children[0].value, 'Remark': myTab.rows[i].cells[5].children[0].value })
            //Details2.push({ 'CustomerName': myTab.rows[i].cells[1].children[0].value, 'ProjectName': myTab.rows[i].cells[2].children[0].value, 'ProjectStatus': myTab.rows[i].cells[3].children[0].value })
            Details2.push({ 'Qualification': myTab.rows[i].cells[0].children[0].value, 'PassingYear': myTab.rows[i].cells[1].children[0].value, 'Division': myTab.rows[i].cells[2].children[0].value, 'Percnet': myTab.rows[i].cells[3].children[0].value, 'Institute': myTab.rows[i].cells[4].children[0].value, 'Remark': myTab.rows[i].cells[5].children[0].value })

        }
        $.ajax({
            cache: false,
            async: true,
            type: "POST",
            url: "@Url.Action("Create", "EmpLoyee")",
            data: frmValues,
            data: JSON.stringify(Details),
            data: JSON.stringify(Details2),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            traditional: true,
            success: function(data) {
                alert(data.FirstName);
            }
        });
    });
});

0 Ответов