加载资源失败:服务器响应状态为500(内部服务器错误)mvc

时间:2018-10-07 12:40:48

标签: javascript jquery json ajax model-view-controller

我正在使用json函数插入主数据和明细数据。 当我单击保存按钮然后在文本框中插入所有数据后 错误显示“ 500”,并且json返回格式有效我已经检查了json 格式验证网站。现在,我正在分享json结果,您可以看到。

<span class="aaa">aaa</span>
<span class="aaa">aaa</span>
<span class="aaa">aaa</span>
<span class="aaa">aaa</span>
<span class="aaa">aaa</span>
<span class="aaa">aaa</span>
<div id = "ex"></div>

我正在分享一切意味着控制器代码,HTML,JavaScript 功能有人告诉我我哪里错了,我的问题是什么 代码。

控制器

{"ItemCode":"001-0001","ItemDesc":"1","PackingDetail":"1","ReOrder_Lvl":"1","Curr_Qty":"1","Curr_Rate":"1","Curr_Value":"1","items":[{"Srno":"1","MUnitCode":"02","BasicUnit_Qty":"1","SaleRate":"1","TableName":"ItemMeasuring"},{"Srno":"1","God_ID":"2","Op_Qty":"1","Op_Amount":"1","TableName":"ItemOpening"}],"btnAddNew":"new"}

型号:

  [HttpPost]

        public ActionResult Items_Insert(string ItemCode, string ItemDesc, string PackingDetail, int ReOrder_Lvl, float Curr_Qty, float Curr_Rate, float Curr_Value, Items[] items,  string btnAddNew)
        {
            string result = "Error! Order Is Not Complete!";
            try
            {
                objclsItems.mItems_Insert(ItemCode, ItemDesc, PackingDetail, ReOrder_Lvl, Curr_Qty, Curr_Value, Curr_Rate, items, btnAddNew);

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

        }

 public int mItems_Insert(string ItemCode, string ItemDesc, string PackingDetail, int ReOrder_Lvl, float Curr_Qty, float Curr_Value, float Curr_Rate, Items[] items, string btnAddNew)
        {
            try
            {
                con.Open();
                tr = con.BeginTransaction();

                if (btnAddNew == "new")
                    cmd = new SqlCommand("Sp_ItemsInsert", con);
                else
                    cmd = new SqlCommand("Sp_ItemsUpdate", con);


                cmd.Parameters.AddWithValue("@ItemCode", ItemCode);
                cmd.Parameters.AddWithValue("@Comp_Id", 1);
                cmd.Parameters.AddWithValue("@ItemDesc", ItemDesc);
                cmd.Parameters.AddWithValue("@PackingDetail", PackingDetail);
                cmd.Parameters.AddWithValue("@ReOrder_Lvl", ReOrder_Lvl);
                cmd.Parameters.AddWithValue("@Curr_Qty", Curr_Qty);
                cmd.Parameters.AddWithValue("@Curr_Value", Curr_Value);
                cmd.Parameters.AddWithValue("@Curr_Rate", Curr_Rate);

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


                if (items != null)
                {
                    for (int i = 0; i < items.Length; i++)
                    {
                        if (items[i].TableName == "ItemMeasuring")
                        {
                            if (btnAddNew == "new")
                                cmd = new SqlCommand("Sp_ItemMeasuringInsert", con);
                            else
                                cmd = new SqlCommand("Sp_ItemMeasuringUpdate", con);

                            cmd.Parameters.AddWithValue("@ItemCode", ItemCode);
                            cmd.Parameters.AddWithValue("@Comp_ID", 1);
                            cmd.Parameters.AddWithValue("@MUnitCode", items[i].MUnitCode);
                            cmd.Parameters.AddWithValue("@BasicUnit_Qty", items[i].BasicUnit_Qty);
                            cmd.Parameters.AddWithValue("@SaleRate", items[i].SaleRate);

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

                        else if (items[i].TableName == "ItemOpening")
                        {
                            if (btnAddNew == "new")
                                cmd = new SqlCommand("Sp_ItemOpeningInsert", con);
                            else
                                cmd = new SqlCommand("Sp_ItemOpeningUpdate", con);

                            cmd.Parameters.AddWithValue("@ItemCode", ItemCode);
                            cmd.Parameters.AddWithValue("@Comp_ID", 1);
                            cmd.Parameters.AddWithValue("@GL_Year", "2018-2019");
                            cmd.Parameters.AddWithValue("@God_ID", items[i].God_ID);
                            cmd.Parameters.AddWithValue("@Op_Qty", items[i].Op_Qty);
                            cmd.Parameters.AddWithValue("@Op_Amount", items[i].Op_Amount);

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

JavaScript

public string ItemCode { get; set; }
        public int Comp_Id { get; set; }
        public string ItemDesc { get; set; }

        public string PackingDetail { get; set; }
        public int ReOrder_Lvl { get; set; }
        public float Curr_Qty { get; set; }
        public float Curr_Value { get; set; }
        public float Curr_Rate { get; set; }

        public string MUnitCode { get; set; }
        public float BasicUnit_Qty { get; set; }
        public float SaleRate { get; set; }

        public float Op_Qty { get; set; }
        public float Op_Amount { get; set; }
        public int God_ID { get; set; }

HTML:

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


    // Add Multiple Record (Item Measuring)

    $("#addToListitemmeasureing").click(function (e) {
        e.preventDefault();

        if ($.trim($("#MUnitCode").val()) == "" || $.trim($("#BasicUnit_Qty").val()) == "" || $.trim($("#SaleRate").val()) == "") return;


        var Srno = document.getElementById("detailsTableitemMeasuring").rows.length,
            MUnitCode = $("#MUnitCode").val(),
            BasicUnit_Qty = $("#BasicUnit_Qty").val(),
            SaleRate = $("#SaleRate").val(),
            detailsTableBodyMeasuring = $("#detailsTableitemMeasuring tbody");
        var ItemsMeasuring = '<tr><td>' + Srno + '</td><td>' + MUnitCode + '</td><td>' + BasicUnit_Qty + '</td><td>' + SaleRate + '</td><td> <a data - itemId="0" href= "#" class="deleteItem" > Remove</a ></td ></tr > ';
        detailsTableBodyMeasuring.append(ItemsMeasuring);
        clearItemMeasuring();
    });

    // Add Multiple Record (Item Opening)

    $("#addToListItemOpening").click(function (e) {
        e.preventDefault();

        if ($.trim($("#txtNGod_ID").val()) == "" || $.trim($("#Op_Qty").val()) == "" || $.trim($("#Op_Amount").val()) == "") return;


        var Srno = document.getElementById("detailsTableItemOpening").rows.length,
            God_ID = $("#txtNGod_ID").val(),
            Op_Qty = $("#Op_Qty").val(),
            Op_Amount = $("#Op_Amount").val(),
            detailsTableItemOpening = $("#detailsTableItemOpening tbody");
        var ItemsOpening = '<tr><td>' + Srno + '</td><td>' + God_ID + '</td><td>' + Op_Qty + '</td><td>' + Op_Amount + '</td><td> <a data-itemId="0" href = "#" class="deleteItem" > Remove</a ></td ></tr > ';
        detailsTableItemOpening.append(ItemsOpening);
        clearItemOpening();

    });

    //After Add A New Order In The List
    function clearItemMeasuring() {
        $("#MUnitCode").val('');
        $("#BasicUnit_Qty").val('');
        $("#SaleRate").val('');

    }

    function clearItemOpening() {
        $("#txtNGod_ID").val('');
        $("#Op_Qty").val('');
        $("#Op_Amount").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", "white").fadeOut(800, function () {
                $(this).remove();
            });
        }
    });
    //After Click Save Button Pass All Data View To Controller For Save Database

    function saveItems(data) {

        return $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'POST',
            url: "@Url.Action("Items_Insert")",
            data: data,
            success: function (result) {

                JSON.parse(results);

                location.reload();
            },
            fail: function (jqXHR, textStatus, errorThrown) {
                console.log('Could not get posts, server response: ' + textStatus + ': ' + errorThrown);
            }

        }).responseJSON;

    }

    //Collect Multiple Order List For Pass To Controller (Item Measuring)

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

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

            itemMeasuringArr.push({
                Srno: $(this).find('td:eq(0)').html(),
                MUnitCode: $(this).find('td:eq(1)').html(),
                BasicUnit_Qty: $(this).find('td:eq(2)').html(),
                SaleRate: $(this).find('td:eq(3)').html(),
                TableName: 'ItemMeasuring'
            });
        });
            $.each($("#detailsTableItemOpening tbody tr"), function () {

                itemMeasuringArr.push({
                    Srno: $(this).find('td:eq(0)').html(),
                    God_ID: $(this).find('td:eq(1)').html(),
                    Op_Qty: $(this).find('td:eq(2)').html(),
                    Op_Amount: $(this).find('td:eq(3)').html(),
                    TableName: 'ItemOpening'
                });
            });

            var data = JSON.stringify({
                ItemCode: $("#txtNItemCode").val(),
                ItemDesc: $("#txtNItemDesc").val(),
                PackingDetail: $("#txtNPackingDetail").val(),
                ReOrder_Lvl: $("#txtNReOrderLvl").val(),
                Curr_Qty: $("#txtNCurrQty").val(),
                Curr_Rate: $("#txtNCurrRate").val(),
                Curr_Value: $("#txtNValue").val(),
                items: itemMeasuringArr,

                btnAddNew: $("#btnAddNew").val()
            });

            //Collect Multiple Order List For Pass To Controller (Item Opening)

            $.when(saveItems(data)).then(function (response) {
                console.log(response);
            }).fail(function (err) {
                console.log(err);
            });
        });


</script>

1 个答案:

答案 0 :(得分:0)

JSON数据所有属性的

类型必须与您向其发送数据的INNER函数的参数类型相对应。在您的示例中,您发送JSON数据

Items_Insert

其中所有属性均为字符串。另一方面,您使用

{
    "ItemCode": "001-0001",
    "ItemDesc": "1",
    "PackingDetail": "1",
    "ReOrder_Lvl": "1",
    "Curr_Qty": "1",
    "Curr_Rate": "1",
    "Curr_Value": "1",
    "items": [{
        "Srno": "1",
        "MUnitCode": "02",
        "BasicUnit_Qty": "1",
        "SaleRate": "1",
        "TableName": "ItemMeasuring"
    }, {
        "Srno": "1",
        "God_ID": "2",
        "Op_Qty": "1",
        "Op_Amount": "1",
        "TableName": "ItemOpening"
    }],
    "btnAddNew": "new"
}

public ActionResult Items_Insert(string ItemCode, string ItemDesc, string PackingDetail, int ReOrder_Lvl, float Curr_Qty, float Curr_Rate, float Curr_Value, Items[] items, string btnAddNew) 的其他一些属性,它们也不是字符串

Items

因此,您必须将另一个格式的数据发送到服务器,或将某些参数和属性的类型从public float BasicUnit_Qty { get; set; } public float SaleRate { get; set; } public float Op_Qty { get; set; } public float Op_Amount { get; set; } public int God_ID { get; set; } 的{​​{1}}更改为字符串。

我提醒您,数字的JSON序列化是不加引号,例如int而不是float

要在客户端解决此问题,您需要像这样发送数据

1

因此,您需要在JavaScript代码的某些位置添加"1"{ "ItemCode": "001-0001", "ItemDesc": "1", "PackingDetail": "1", "ReOrder_Lvl": 1, "Curr_Qty": 1, "Curr_Rate": 1, "Curr_Value": 1, "items": [{ "Srno": "1", "MUnitCode": "02", "BasicUnit_Qty": 1, "SaleRate": 1, "TableName": "ItemMeasuring" }, { "Srno": "1", "God_ID": 2, "Op_Qty": 1, "Op_Amount": 1, "TableName": "ItemOpening" }], "btnAddNew": "new" } 。例如,您应该使用

parseInt

代替

parseFloat

用于当前代码。您必须对所有属性进行仔细更改,在服务器代码中将其解释为整数而不是字符串的浮点数。