首页 > 解决方案 > json函数不将值传递给控制器

问题描述

您好,我在数据库中插入数据一切都很完美,但是当我单击保存按钮时,json 函数没有调用控制器。当我使用断点并检查数据中传递了哪些值时,这里是数据中显示的值

当我检查浏览器控制台时,只显示错误 undefine 。json 函数返回此值

  "{"PO_ID":"60039","PONo":"000036","PODate":"28/09/2018","RequisitionDate":"28/09/2018","DeliveryDate":"28/09/2018","IsCancelled":"true","IsClosed":"true","IsPending":"true","Party_Code":null,"SellerQuotRefNo":"21","SellerQuotDate":"28/09/2018","ComparativeRefNo":"32","WithItemCode":"on","ValidateQty":"true","TermsOfDelivery":"a","TermsOfPayment":"b","TermsOfConditions":"c","items":[{"Srno":"1","InvTypeID":"1","ItemCode":"018-0002","MUnitCode":"03","Specfication":"aaa","Qty":"21","Rate":"43","Amount":"444","STax":"55","STaxAmount":"556","GrossAmount":"223333"}],"AddNew":"1"}"

控制器

 [HttpPost]
        public ActionResult POInsert(string PO_ID, string PONo, string PODate, string RequisitionDate, string DeliveryDate, bool IsCancelled, bool IsClosed, bool IsPending, string Party_Code, string SellerQuotRefNo, string SellerQuotDate, string ComparativeRefNo, bool WithItemCode, bool ValidateQty, string TermsOfDelivery, string TermsOfPayment, string TermsOfConditions, PurchaseOrder[] items, string AddNew)
        {
            string result = "Error! Order Is Not Complete!";
            try
            {
                objclsPurchaseOrder.PurchaseOrderInsert(PO_ID, PONo, PODate, RequisitionDate, DeliveryDate, IsCancelled, IsClosed, IsPending, Party_Code, SellerQuotRefNo, SellerQuotDate, ComparativeRefNo, WithItemCode, ValidateQty, TermsOfDelivery, TermsOfPayment, TermsOfConditions, items, AddNew);

                //ViewBag.Message = "Your record has been inserted Successfully";
                ModelState.Clear();

                result = "Your record has been inserted Successfully!";

                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {

                throw;
            }

        }

jQuery

<script>
    //Show model
    function addNewOrder() {
        $("#NewOrderForm").modal();
    }


    // Add Multiple Record
    $("#addToList").click(function (e) {
        e.preventDefault();

        if ($.trim($("#InvTypeID").val()) == "" || $.trim($("#ItemCode").val()) == "" || $.trim($("#MUnitCode").val()) == "" || $.trim($("#Specfication").val()) == "" || $.trim($("#Qty").val()) == "" || $.trim($("#Rate").val()) == "" || $.trim($("#Amount").val()) == "" || $.trim($("#STax").val()) == "" || $.trim($("#STaxAmount").val()) == "" || $.trim($("#GrossAmount").val()) == "") return;


        var Srno = document.getElementById("detailsTable").rows.length,
            InvTypeID = $("#InvTypeID").val(),
            ItemCode = $("#ItemCode").val(),
            MUnitCode = $("#MUnitCode").val(),
            Specfication = $("#Specfication").val(),
            Qty = $("#Qty").val(),
            Rate = $("#Rate").val(),
            Amount = $("#Amount").val(),
            STax = $("#STax").val(),
            STaxAmount = $("#STaxAmount").val(), 
            GrossAmount = $("#GrossAmount").val(),
            detailsTableBody = $("#detailsTable tbody");
        var ReqItems = '<tr><td>' + Srno + '</td><td>' + InvTypeID + '</td><td>' + ItemCode + '</td><td>' + MUnitCode + '</td><td>' + Specfication + '</td><td>' + Qty + '</td><td>' + Rate + '</td><td>' + Amount + '</td><td>' + STax + '</td><td>' + STaxAmount + '</td><td>' + GrossAmount+ '</td><td> <a data-itemId="0" href="#" class="deleteItem">Remove</a></td></tr>';
        detailsTableBody.append(ReqItems);
        clearItem();
    });
    //After Add A New Order In The List
    function clearItem() {
        $(".chosen-select").val('');

        $("#Specfication").val('');
        $("#Qty").val('');
        $("#Rate").val('');
        $("#Amount").val('');
        $("#STax").val('');
        $("#STaxAmount").val('');
        $("#GrossAmount").val('');

    }


    // After Add A New Order In The List, If You Want, You Can Remove

    $(document).on('click', 'a.deleteItem', function (e) {
        e.preventDefault();
        var $self = $(this);
        if ($(this).attr('data-itemId') == "0") {
            $(this).parents('tr').css("background-color", "Red").fadeOut(800, function () {
                $(this).remove();
            });
        }
    });
    //After Click Save Button Pass All Data View To Controller For Save Database

    function savePO(data) {
        if ($.trim($("#Party_Code").val()) == "" || $.trim($("#SellerQuotRefNo").val()) == "" || $.trim($("#ComparativeRefNo").val()) == "") return;

        return $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'POST',
            url: "/Home/POInsert",
            data: data,
            success: function (result) {
                alert(result);
                location.reload();
            },
            error: function () {
                alert("Error!")
            }
        });

    }

    //Collect Multiple Order List For Pass To Controller

    $("#savePO").click(function (e) {
        e.preventDefault();
        var requisitionArr = [];
        requisitionArr.length = 0;

        $.each($("#detailsTable tbody tr"), function () {

            requisitionArr.push({
                Srno: $(this).find('td:eq(0)').html(),
                InvTypeID: $(this).find('td:eq(1)').html(),
                ItemCode: $(this).find('td:eq(2)').html(),
                MUnitCode: $(this).find('td:eq(3)').html(),
                Specfication: $(this).find('td:eq(4)').html(),
                Qty: $(this).find('td:eq(5)').html(),
                Rate: $(this).find('td:eq(6)').html(),
                Amount: $(this).find('td:eq(7)').html(),
                STax: $(this).find('td:eq(8)').html(),
                STaxAmount: $(this).find('td:eq(9)').html(),
                GrossAmount: $(this).find('td:eq(10)').html()

            });
        });

        var data = JSON.stringify({
            PO_ID: $("#POID").val(),
            PONo: $("#PONO").val(),
            PODate: $("#PODate").val(),
            RequisitionDate: $("#RequisitionDate").val(),
            DeliveryDate: $("#DeliveryDate").val(),
            IsCancelled: $("#IsCancelled").val(),

            IsClosed: $("#IsClosed").val(),
            IsPending: $("#IsPending").val(),
            Party_Code: $("#PartyCode").val(),
            SellerQuotRefNo: $("#SellerQuotRefNo").val(),
            SellerQuotDate: $("#SellerQuotDate").val(),
            ComparativeRefNo: $("#ComparativeRefNo").val(),
            WithItemCode: $("#WithItemCode").val(),
            ValidateQty: $("#ValidateQty").val(),
            TermsOfDelivery: $("#TermsOfDelivery").val(),
            TermsOfPayment: $("#TermsOfPayment").val(),
            TermsOfConditions: $("#TermsOfConditions").val(),
            items: requisitionArr,
            AddNew: $("#AddNew").val()
        });

        $.when(savePO(data)).then(function (response) {
            console.log(response);
        }).fail(function (err) {
            console.log(err);
        });
    });
</script>

HTML

<div class="modal fade" id="centralModalLGInfoDemo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div class="modal-dialog modal-lg modal-notify modal-info" role="document">
                <!--Content-->

                <div class="modal-content" style="width:140%">
                    <!--Header-->
                    <div class="modal-header">
                        <p class="heading lead">Add New Purchase Order</p>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true" class="white-text">&times;</span>
                        </button>
                    </div>
                    <!--Body-->
                    <form id="NewOrderForm">
                        <div class="modal-body">
                            <div class="form-row">
                                <div class="col" id="poidd">
                                    <!-- Purchase PO ID -->
                                    <div class="md-form">
                                        @Html.TextBox("PO_ID", (string)ViewBag.PO_ID, new { @class = "form-control mr-sm-3", @id = "POID", Required = true })
                                    </div>
                                </div>
                                <div class="col">
                                    <!-- Purchase PO NO -->
                                    <div class="md-form">
                                        @Html.TextBox("PONo", (String)ViewBag.PONO, new { @class = "form-control mr-sm-3", @id = "PONO", Required = true })

                                        <label for="lblPONo">PO No.</label>
                                    </div>
                                </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                <div class="col">
                                    <!-- PO Date -->
                                    <div class="md-form">

                                        @Html.TextBoxFor(m => m.PODate, new { @class = "form-control", @id = "PODate", Required = true })

                                        <label for="lblPODatepicker"> PO Date</label>
                                    </div>
                                </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                <div class="col">
                                    <!-- Requisition Date -->
                                    <div class="md-form">

                                        @Html.TextBoxFor(m => m.RequisitionDate, new { @class = "form-control", @id = "RequisitionDate", Required = true })

                                        <label for="lblRequisitionDatepicker"> Requisition Date</label>
                                    </div>
                                </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                <div class="col">
                                    <!-- Delivery Date -->
                                    <div class="md-form">

                                        @Html.TextBoxFor(m => m.DeliveryDate, new { @class = "form-control", @id = "DeliveryDate", Required = true })

                                        <label for="lblDeliveryDatepicker"> Delivery Date</label>
                                    </div>
                                </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


                                <div class="col">
                                    <!-- IsCancel-->
                                    <div class="custom-control custom-checkbox custom-control-inline">
                                      @Html.CheckBoxFor(m => m.IsCancelled, new { @class = "custom-control-input", @id= "IsCancelled" })

                                        <label class="custom-control-label" for="IsCancelled">Cancel</label>
                                    </div>

                                    <!-- IsClosed-->
                                    <div class="custom-control custom-checkbox custom-control-inline">
                                        @Html.CheckBoxFor(m => m.IsClosed, new { @class = "custom-control-input", @id = "IsClosed" })

                                        <label class="custom-control-label" for="IsClosed">Closed</label>
                                    </div>

                                    <!-- IsPending-->
                                    <div class="custom-control custom-checkbox custom-control-inline">
                                        @Html.CheckBoxFor(m => m.IsPending, new { @class = "custom-control-input", @id = "IsPending" })

                                        <label class="custom-control-label" for="IsPending">Pending</label>
                                    </div>

                                </div>



                            </div>

                            <div class="form-row">
                                <div class="col">
                                    <!-- Party Code -->
                                    <div class="md-form">
                                       @Html.DropDownListFor(m => m.Party_Code, ViewBag.PartyCode as List<SelectListItem>, new { @class = "chosen-select", id = "PartyCode", Required = true })
                                    </div>
                                </div>&nbsp;&nbsp;&nbsp;&nbsp;
                                <div class="col" style="margin-left:1.5%;">
                                    <!-- SellerQuotRefNo -->
                                    <div class="md-form">
                                        @Html.TextBoxFor(m => m.SellerQuotRefNo, new { @class = "form-control", @id = "SellerQuotRefNo", @placeholder = "SellerQuotRefNo", Required = true })

                                        <label for="lblSellerQuotRefNo">Seller Quot Ref No.</label>
                                    </div>
                                </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                <div class="col" style="margin-left:1.5%;">
                                    <!--SellerQuotDate -->
                                    <div class="md-form">

                                        @Html.TextBoxFor(m => m.SellerQuotDate, new { @class = "form-control", @id = "SellerQuotDate", Required = true })

                                        <label for="lblSellerQuotDateDatepicker"> Seller Quot Date</label>
                                    </div>
                                </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                <div class="col" style="margin-left:1.8%;">
                                    <!-- ComparativeRefNo -->
                                    <div class="md-form">

                                        @Html.TextBoxFor(m => m.ComparativeRefNo, new { @class = "form-control", @id = "ComparativeRefNo", @placeholder = "ComparativeRefNo", Required = true })

                                        <label for="lblComparativeRefNo"> Comparative RefNo</label>
                                    </div>
                                </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                <div class="col">

                                    <!-- WithItemCode-->
                                    <div class="custom-control custom-radio">

                                        <input type="radio" class="custom-control-input" id="WithItemCode" name="groupOfDefaultRadios">
                                        <label class="custom-control-label" for="WithItemCode">WithItemCode</label>
                                    </div>

                                    <!-- With GL Code -->
                                    <div class="custom-control custom-radio">
                                        <input type="radio" class="custom-control-input" id="WithGLCode" name="groupOfDefaultRadios" checked>
                                        <label class="custom-control-label" for="WithGLCode">With GL Code</label>
                                    </div>
                                    <!-- IsPending-->
                                    <div class="custom-control custom-checkbox custom-control-inline">
                                        @Html.CheckBoxFor(m => m.ValidateQty, new { @class = "custom-control-input", @id = "ValidateQty" })

                                        <label class="custom-control-label" for="ValidateQty">Validate Qty</label>
                                    </div>

                                </div>

                            </div>

                            <div class="form-row">

                                <div class="col" style="margin-left:1.5%;">
                                    <!--TermsOfDelivery -->
                                    <div class="md-form">

                                        @Html.TextBoxFor(m => m.TermsOfDelivery, new { @class = "form-control", @id = "TermsOfDelivery", @placeholder = "TermsOfDelivery", Required = true })

                                        <label for="lblTermsOfDelivery"> Terms Of Delivery</label>
                                    </div>
                                </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                <div class="col" style="margin-left:1.8%;">
                                    <!--TermsOfPayment -->
                                    <div class="md-form">

                                        @Html.TextBoxFor(m => m.TermsOfPayment, new { @class = "form-control", @id = "TermsOfPayment", @placeholder = "TermsOfPayment", Required = true })

                                        <label for="lblTermsOfPayment"> Terms Of Payment</label>
                                    </div>
                                </div>

                                <div class="col" style="margin-left:1.8%;">
                                    <!-- TermsOfConditions -->
                                    <div class="md-form">

                                        @Html.TextBoxFor(m => m.TermsOfConditions, new { @class = "form-control", @id = "TermsOfConditions", @placeholder = "TermsOfConditions", Required = true })

                                        <label for="lblTermsOfConditions">Terms Of Conditions</label>
                                    </div>
                                </div>




                            </div>

                            <!--Detail-->
                            <h5 style="margin-top:10px;color:#ff6347">PO Details</h5>
                            <hr />
                            <div>
                                <div class="form-row">

                                    <!-- InvTypeID -->

                                    <div class="md-form">
                                        @Html.DropDownListFor(m => m.InvTypeID, ViewBag.InvoiceType as List<SelectListItem>, new { @class = "chosen-select", id = "InvTypeID", Required = true })

                                    </div>
                                    <div class="md-form">
                                        @Html.DropDownListFor(m => m.ItemCode, ViewBag.Items as List<SelectListItem>, new { @class = "chosen-select", id = "ItemCode", Required = true })

                                    </div>
                                    <div class="md-form">
                                        @Html.DropDownListFor(m => m.MUnitCode, ViewBag.munit as List<SelectListItem>, new { @class = "chosen-select", id = "MUnitCode", Required = true })

                                    </div>
                                    <div class="col">
                                        <!-- Job -->
                                        <div class="md-form">
                                            <input type="text" id="Specfication" name="Specfication" placeholder="Specfication" class="form-control" ,Required=true />

                                            <label for="lbljob">Specfication</label>
                                        </div>
                                    </div>

                                    <div class="col-md-2 col-lg-offset-4">
                                        <a id="addToList" class="btn btn-primary">Add To List</a>
                                    </div>
                                </div>

                                <div class="form-row">
                                    <div class="col">
                                        <!-- Qty -->
                                        <div class="md-form">
                                            <input type="number" id="Qty" name="Qty" placeholder="Qty" class="form-control" ,Required=true />

                                            <label for="lblQty">Qty</label>
                                        </div>
                                    </div>
                                    <div class="col">
                                        <!-- Rate -->
                                        <div class="md-form">
                                            <input type="number" id="Rate" name="Rate" placeholder="Rate" class="form-control" ,Required=true />

                                            <label for="lblRate">Rate</label>
                                        </div>
                                    </div>
                                    <div class="col">
                                        <!-- Amount -->
                                        <div class="md-form">
                                            <input type="number" id="Amount" name="Amount" placeholder="Amount" class="form-control" ,Required=true />

                                            <label for="lbljob">Amount</label>
                                        </div>
                                    </div>
                                    <div class="col">
                                        <!-- S.Tax% -->
                                        <div class="md-form">
                                            <input type="number" id="STax" name="STax" placeholder="STax" class="form-control" ,Required=true />

                                            <label for="lbljob">S.Tax%</label>
                                        </div>
                                    </div>
                                    <div class="col">
                                        <!-- STaxAmount -->
                                        <div class="md-form">
                                            <input type="number" id="STaxAmount" name="STaxAmount" placeholder="STaxAmount" class="form-control" ,Required=true />

                                            <label for="lbljob">S.Tax Amount</label>
                                        </div>
                                    </div>
                                    <div class="col">
                                        <!-- Job -->
                                        <div class="md-form">
                                            <input type="number" id="GrossAmount" name="GrossAmount" placeholder="GrossAmount" class="form-control" ,Required=true />

                                            <label for="lbljob">Gross Amount</label>
                                        </div>
                                    </div>

                                </div>
                                <table id="detailsTable" class="table">
                                    <thead style="background-color:#007bff; color:white">
                                        <tr>
                                            <th style="width:2%">SrNo.</th>
                                            <th style="width:8%">Invoice Type</th>
                                            <th style="width:8%">Items</th>
                                            <th style="width:8%">MUnit</th>
                                            <th style="width:10%">Specification</th>
                                            <th style="width:3%">Qty</th>
                                            <th style="width:3%">Rate</th>
                                            <th style="width:5%">Amount</th>
                                            <th style="width:4%">STax%</th>
                                            <th style="width:4%">STaxAmount</th>
                                            <th style="width:5%">Gross Amount</th>

                                            <th style="width:5%"></th>
                                        </tr>
                                    </thead>
                                    <tbody></tbody>
                                </table>
                            </div>
                            <div class="modal-footer">
                                <button type="reset" class="btn btn-primary" data-dismiss="modal">Close</button>
                                <button id="savePO" type="submit" class="btn btn-primary">Save Record</button>
                            </div>
                        </div>
                    </form>



                </div>

                <!--/.Content-->
            </div>
        </div>

标签: javascriptc#jqueryjsonasp.net-mvc

解决方案


尝试使用类 RootObject 的对象作为 POInsert 动作中的参数

public class Item
{
    public string Srno { get; set; }
    public string InvTypeID { get; set; }
    public string ItemCode { get; set; }
    public string MUnitCode { get; set; }
    public string Specfication { get; set; }
    public string Qty { get; set; }
    public string Rate { get; set; }
    public string Amount { get; set; }
    public string STax { get; set; }
    public string STaxAmount { get; set; }
    public string GrossAmount { get; set; }
}

public class RootObject
{
    public string PO_ID { get; set; }
    public string PONo { get; set; }
    public string PODate { get; set; }
    public string RequisitionDate { get; set; }
    public string DeliveryDate { get; set; }
    public string IsCancelled { get; set; }
    public string IsClosed { get; set; }
    public string IsPending { get; set; }
    public object Party_Code { get; set; }
    public string SellerQuotRefNo { get; set; }
    public string SellerQuotDate { get; set; }
    public string ComparativeRefNo { get; set; }
    public string WithItemCode { get; set; }
    public string ValidateQty { get; set; }
    public string TermsOfDelivery { get; set; }
    public string TermsOfPayment { get; set; }
    public string TermsOfConditions { get; set; }
    public List<Item> items { get; set; }
    public string AddNew { get; set; }
}

推荐阅读