json函数不能在mvc中使用jquery ajax调用

时间:2021-03-24 11:24:49

Hello i want to insert data through json function but when i click save button then json function not calling mItem_Insert function and show error on front end Error: function Error() { [native code] } and i am using tab page contain 2 tab one for items and other is Item category and also shown on this page jqgrid . kindly anyone tell me what is the problem in this code and why this error show .

你好我想通过json函数插入数据,但是当我点击保存按钮然后json函数没有调用mItem_Insert函数并在前端显示错误错误:函数Error(){[native code]}我正在使用tab页面包含2 tab 1对于项目和其他是项目类别,也显示在此页面jqgrid。请有人告诉我这段代码中的问题是什么以及此错误显示的原因。

Javascript

<script>
     function mSaveItem() {

            $.ajax({
                url: "@Url.Action("mItem_Insert")",
                type: "POST",
                contentType: "application/json;charset=UTF-8",
                data: {
                    CatCode:$('#txtNCatCode').val(),
                    CatDesc: $('#txtNCategoryDesc').val(),
                    HS_Code: $('#txtNHsCode').val(),
                    PurCode: $('#txtNPurCode').val(),
                    SaleCode: $('#txtNSaleCode').val(),
                    CostOfSaleCode: $('#txtNCostOfSaleCode').val(),
                    SaleCode_Trade:$('#txtNSaleCode_Trade').val(),
                    CostOfSaleCode_Trade: $('#txtNCostOfSaleCode_Trade').val()

                },
                dataType: "json",
                success: function (Record) {

                    if (Record.d == true) {

                        $('#Result').text("Your Record inserted");
                    }
                    else {
                        $('#Result').text("Your Record Not Insert");
                    }

                },
                error: function (errormessage) {
                    $('#Result').text("Error: " + Error);
                }
            });
            return false;
        }

</script>

HTML

<main class="pt-5 mx-lg-5">
    <div class="container-fluid mt-5">

        <!-- Nav tabs -->
        <ul class="nav nav-tabs md-tabs nav-justified indigo" role="tablist" >
            <li class="nav-item" style="background-color:#007bff">
                <a class="nav-link active" data-toggle="tab" href="#panel5" role="tab"> Items</a>
            </li>
            <li class="nav-item" style="background-color:#007bff">
                <a class="nav-link" data-toggle="tab" href="#panel6" role="tab"><span class="glyphicon glyphicon-folder-open"></span> Items Category</a>
            </li>

        </ul>


        <!-- Items Tab panels -->



        <div class="tab-content">
            <!--Panel 1-->
            <div class="tab-pane fade in show active" id="panel5" role="tabpanel">
                <br>
                <button type="button" id="AddNew" value="1" class="btn btn-primary" data-toggle="modal" data-target="#centralModalLGInfoDemo" style="float:left">Add New</button>

                <div class="table-responsive">
                    <table id="jqGrid" class="table table-bordered table-striped"></table>
                    <div id="jqGridPager"></div>
                </div>
            </div>
            <!--/.Panel 1-->

            <!--Items Model-->



            <!--Panel 2-->
            <div class="tab-pane fade" id="panel6" role="tabpanel">
                <br>
                <button type="button" id="AddNew" value="1" class="btn btn-primary" data-toggle="modal" data-target="#ItemModel" style="float:left">Add New Category</button>

                <div class="table-responsive">
                    <table id="jqItemCatgoryGrid" class="table table-bordered table-striped"></table>
                    <div id="jqGridItemCatgoryPager"></div>
                </div>

                <form action="" method="post">
                    <div class="modal fade left" id="ItemModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
                         data-backdrop="false">
                        <div class="modal-dialog modal-side modal-top-left modal-notify modal-info" role="document">
                            <!--Content-->
                            <div class="modal-content" style="margin-left:68%;">
                                <!--Header-->
                                <div class="modal-header">
                                    <p class="heading lead">Add New Item Category</p>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true" class="white-text">&times;</span>
                                    </button>
                                </div>
                                <!--Body-->

                                <div class="modal-body" style="width:80%">


                                    <h3 id="Result"></h3>

                                    <div class="form-row">
                                        <div class="col">
                                            <!-- Requisition Req_NO -->
                                            <div class="md-form">
                                                <label id="txtDetailReq_ID">Category Code</label>
                                                @Html.TextBox("CatCode", (string)ViewBag.Category, new { @class = "form-control mr-sm-3", @id = "txtNCatCode", Required = true })
                                            </div>
                                        </div>
                                        <div class="col">
                                            <!-- Requisition Date -->
                                            <div class="md-form">

                                                @Html.TextBoxFor(m => m.CatDesc, new { @class = "form-control mr-sm-3", @id = "txtNCategoryDesc", @placeholder = "Item Description", Required = true })

                                                <label for="lblItemDesc">Category Description</label>
                                            </div>
                                        </div>


                                    </div>

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

                                                @Html.TextBoxFor(m => m.HS_Code, new { @class = "form-control mr-sm-3", @id = "txtNHsCode", @placeholder = "HS Code" })

                                                <label for="lblHsCode">HS Code</label>
                                            </div>
                                        </div>

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

                                                @Html.TextBoxFor(m => m.PurCode, new { @class = "form-control mr-sm-3", @id = "txtNPurCode", @placeholder = "Pur Code", Required = true })

                                                <label for="lblRequisitionDatepicker">Pur Code</label>
                                            </div>
                                        </div>
                                        <div class="col">
                                            <!-- Job -->
                                            <div class="md-form">

                                                <input type="text" id="PurCodeDescription" class="form-control mr-sm-5" />
                                                <label for="lbljob">Description</label>
                                            </div>
                                        </div>

                                    </div>

                                    <div class="form-row">
                                        <div class="col">

                                            <div class="md-form">

                                                @Html.TextBoxFor(m => m.SaleCode, new { @class = "form-control mr-sm-3", @id = "txtNSaleCode", @placeholder = "Sale Code" })

                                                <label for="lblNsaleCode">Sale Code</label>
                                            </div>
                                        </div>
                                        <div class="col">
                                            <!-- Job -->
                                            <div class="md-form">
                                                <input type="text" id="SaleCodeDescription" class="form-control mr-sm-5" />
                                                <label for="lblSaleCodeDesc">Description</label>
                                            </div>
                                        </div>
                                    </div>

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

                                                @Html.TextBoxFor(m => m.CostOfSaleCode, new { @class = "form-control mr-sm-3", @id = "txtNCostOfSaleCode", @placeholder = "Cost Of Sale Code" })

                                                <label for="lblNCostOfSaleCode">Cost of Sale Code</label>
                                            </div>
                                        </div>
                                        <div class="col">
                                            <!-- Job -->
                                            <div class="md-form">
                                                <input type="text" id="CostOfSaleCodeDescription" class="form-control mr-sm-5" />
                                                <label for="lblNCostOfSaleCodeDescription">Description</label>
                                            </div>
                                        </div>
                                    </div>

                                    <h6 style="color:black; display:none">For Trade Only</h6>

                                    <div class="form-row" style="display:none">
                                        <div class="col">

                                            <div class="md-form">

                                                @Html.TextBoxFor(m => m.SaleCode_Trade, new { @class = "form-control mr-sm-3", @id = "txtNSaleCode_Trade", @placeholder = "Sale Code Trade" })

                                                <label for="lblNSaleCode_Trade">Sale Code</label>
                                            </div>
                                        </div>
                                        <div class="col">
                                            <!-- Job -->
                                            <div class="md-form">
                                                <input type="text" id="SaleCodeDescriptionTrad" class="form-control mr-sm-5" />
                                                <label for="lblNCostOfSaleCodeDescriptionTrade">Description</label>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-row" style="display:none">
                                        <div class="col">

                                            <div class="md-form">

                                                @Html.TextBoxFor(m => m.CostOfSaleCode_Trade, new { @class = "form-control mr-sm-3", @id = "txtNCostOfSaleCode_Trade", @placeholder = "Cost Of Sale Code Trade" })

                                                <label for="lblNCostOfSaleCodeTrade">Cost Of Sale Code Trade</label>
                                            </div>
                                        </div>
                                        <div class="col">
                                            <!-- Job -->
                                            <div class="md-form">
                                                <input type="text" id="CostSaleCodeDescT" class="form-control mr-sm-5" />
                                                <label for="lblNCostOfSaleCodeTradeDesc">Description</label>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="justify-content-center">
                                        <div class="modal-footer">
                                            <button type="reset" class="btn btn-default" data-dismiss="modal">Close</button>
                                            <button id="btnItemSave" type="submit" class="btn btn-danger " onclick="mSaveItem()">Save</button>
                                        </div>
                                    </div>


                                </div>

                                <!-- Default form contact -->

                            </div>
                            <!--Footer-->

                        </div>
                        <!--/.Content-->
                    </div>
                </form>
            </div>
            <!--/.Panel 2-->





            </div>


        </div>
    </main>

Json Function In C# #region Items Insert

C ##region项插入中的Json函数

      public JsonResult mItem_Insert(Items objItem)
        {

            try
            {
                objclsItems.ItemsInsert(objItem);
                ModelState.Clear();

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

                throw;
            }

        }
  public int ItemsInsert(Items mitem)
        {
            try
            {
                con.Open();
                tr = con.BeginTransaction();


                cmd = new SqlCommand("Select Right('000' + Cast(ISNULL(MAX(CatCode),0)+1  as varchar(2)) + '', 3) from ItemCategory", con);
                cmd.Transaction = tr;
               string Cat_Code = cmd.ExecuteScalar().ToString();

                cmd = new SqlCommand("Sp_ItemCategoryInsert", con);

                cmd.Parameters.AddWithValue("@CatCode", Cat_Code);
                cmd.Parameters.AddWithValue("@Comp_Id", 1);

                if (mitem.CatDesc != null)
                    cmd.Parameters.AddWithValue("@CatDesc", mitem.CatDesc);
                else
                    cmd.Parameters.AddWithValue("@CatDesc", string.Empty);

                if (mitem.PurCode != null)
                    cmd.Parameters.AddWithValue("@PurCode", mitem.PurCode);
                else
                    cmd.Parameters.AddWithValue("@PurCode", string.Empty);

                if (mitem.SaleCode != null)
                    cmd.Parameters.AddWithValue("@SaleCode", mitem.SaleCode);
                else
                    cmd.Parameters.AddWithValue("@SaleCode", string.Empty);

                if (mitem.CostOfSaleCode != null)
                    cmd.Parameters.AddWithValue("@CostOfSaleCode", mitem.CostOfSaleCode);
                else
                    cmd.Parameters.AddWithValue("@CostOfSaleCode", string.Empty);

                if (mitem.SaleCode_Trade != null)
                    cmd.Parameters.AddWithValue("@SaleCode_Trade", mitem.SaleCode_Trade);
                else
                    cmd.Parameters.AddWithValue("@SaleCode_Trade", string.Empty);

                if (mitem.CostOfSaleCode_Trade != null)
                    cmd.Parameters.AddWithValue("@CostOfSaleCode_Trade", mitem.CostOfSaleCode_Trade);
                else
                    cmd.Parameters.AddWithValue("@CostOfSaleCode_Trade", string.Empty);

                if (mitem.HS_Code != null)
                    cmd.Parameters.AddWithValue("@HS_Code", mitem.HS_Code);
                else
                    cmd.Parameters.AddWithValue("@HS_Code", string.Empty);


                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Transaction = tr;
                cmd.ExecuteNonQuery();


                tr.Commit();
                return i;
            }
            catch (SqlException sqlex)
            {
                tr.Rollback();
                throw sqlex;  // read all sql error 
            }
            catch (Exception ex)
            {
                tr.Rollback();
                throw ex; // General execption

            }
            finally
            {
                con.Close();
            }
        }

2 个解决方案

#1


0  

ı changed your code with post

ı用帖子改了你的代码

$.post("@Url.Action("mItem_Insert")",
            {
                CatCode: $('#txtNCatCode').val(),
                CatDesc: $('#txtNCategoryDesc').val(),
                HS_Code: $('#txtNHsCode').val(),
                PurCode: $('#txtNPurCode').val(),
                SaleCode: $('#txtNSaleCode').val(),
                CostOfSaleCode: $('#txtNCostOfSaleCode').val(),
                SaleCode_Trade: $('#txtNSaleCode_Trade').val(),
                CostOfSaleCode_Trade: $('#txtNCostOfSaleCode_Trade').val()
            },
            function (record) {
                if (record.d === true) {
                    $('#Result').text("Your Record inserted");
                }
                else {
                    $('#Result').text("Your Record Not Insert");
                }

            }).fail(function(response) {
                $('#Result').text("Error: " + response.responseText);
        });

#2


0  

Your url should be @Url.Action("mItem_Insert","ControllerName"). where controller name is the name of your controller.

您的网址应为@ Url.Action(“mItem_Insert”,“ControllerName”)。其中控制器名称是控制器的名称。

Also instead of using @Url.Action directly in jquery, you can always assign it to an element data-url attribute and in jquery extract that url and use it in ajax:

而不是直接在jquery中使用@ Url.Action,你总是可以将它分配给一个元素data-url属性,并在jquery中提取该url并在ajax中使用它:

<div id="urlDiv" data-url="@Url.Action(blahblah)"></div>

<script>
var url = $('#urlDiv').data('url');
console.log(url);   // test that url is correct

$.ajax({
url:url
...
});
</script>

#1


0  

ı changed your code with post

ı用帖子改了你的代码

$.post("@Url.Action("mItem_Insert")",
            {
                CatCode: $('#txtNCatCode').val(),
                CatDesc: $('#txtNCategoryDesc').val(),
                HS_Code: $('#txtNHsCode').val(),
                PurCode: $('#txtNPurCode').val(),
                SaleCode: $('#txtNSaleCode').val(),
                CostOfSaleCode: $('#txtNCostOfSaleCode').val(),
                SaleCode_Trade: $('#txtNSaleCode_Trade').val(),
                CostOfSaleCode_Trade: $('#txtNCostOfSaleCode_Trade').val()
            },
            function (record) {
                if (record.d === true) {
                    $('#Result').text("Your Record inserted");
                }
                else {
                    $('#Result').text("Your Record Not Insert");
                }

            }).fail(function(response) {
                $('#Result').text("Error: " + response.responseText);
        });

#2


0  

Your url should be @Url.Action("mItem_Insert","ControllerName"). where controller name is the name of your controller.

您的网址应为@ Url.Action(“mItem_Insert”,“ControllerName”)。其中控制器名称是控制器的名称。

Also instead of using @Url.Action directly in jquery, you can always assign it to an element data-url attribute and in jquery extract that url and use it in ajax:

而不是直接在jquery中使用@ Url.Action,你总是可以将它分配给一个元素data-url属性,并在jquery中提取该url并在ajax中使用它:

<div id="urlDiv" data-url="@Url.Action(blahblah)"></div>

<script>
var url = $('#urlDiv').data('url');
console.log(url);   // test that url is correct

$.ajax({
url:url
...
});
</script>