从控制器到视图时Json出错

时间:2015-08-18 20:18:36

标签: jquery ajax json

当我的数据对象返回到我的页面时,某些东西会被呛到。它总是转到我的'错误'功能而不是我的'成功'功能。这是我当前的jquery函数。我想要做的是通过输入UPC在我的页面上填充产品,并且onblur填写corperateitemcode。数据填充在控制器中,但它在页面上出错。这对我来说很棘手的是页面可以有多个产品,所以我不能通过id引用它。任何帮助将不胜感激。

 function FillProductbyupc(elem) {
        var $elem = $(elem),
            upcCode = $elem.data('upc');

        $.ajax({
            url: '/Case/GetData/',
            type: "GET",
            dataType: "JSON",
            data: "upc=" + upcCode,
            success: function (data) {
                $(".corpitemcode").append(
                $elem.find('.corpitemcode').val(data.CorpItemCode));
                $(".itemdesc").append(
                 $elem.find('.itemdesc').val(data.ItemDsc));
            },
            error: function () {
                alert("UPC does not exist");
            }
        });
    }
    $(function () {

        $('.products').on('blur', '[data-upc]', function (e) {
            FillProductbyupc(this);
        });
    });

继承我的产品

<div class="product">

    <p>
        <label>Product</label>
        @Html.TextBoxFor(x => x.UPC, null, new { @class = "form-control", placeholder = Html.DisplayNameFor(x => x.UPC), data_upc = "5440001504" })
        @Html.TextBoxFor(x => x.PLU, null, new { @class = "form-control", placeholder = Html.DisplayNameFor(x => x.PLU) })
        @Html.TextBoxFor(x => x.CorpItemCode, null, new { @class = "form-control corpitemcode", placeholder = Html.DisplayNameFor(x => x.CorpItemCode) })
        @Html.TextBoxFor(x => x.ItemDsc, null, new { @class = "form-control itemdesc", placeholder = Html.DisplayNameFor(x => x.ItemDsc) })
        @Html.TextBoxFor(x => x.LaneNumber, null, new { @class = "form-control", placeholder = Html.DisplayNameFor(x => x.LaneNumber) })

        @Html.HiddenFor(x => x.DeleteProduct, new { @class = "mark-for-delete" })
        @Html.RemoveLink("Remove", "div.product", "input.mark-for-delete")





    </p>
</div>

我的控制器

 public JsonResult GetData(string upc, decimal? corpitemcode)
    {
        if (!string.IsNullOrEmpty(upc))
        {
          upc = upc.Replace("-", "");
        }

        CICS data = new CICS();
        data = CICSManager.GetbyUPCorCorp(upc, corpitemcode);

        return Json(data, JsonRequestBehavior.AllowGet);
    }

0 个答案:

没有答案