json函数在mvc中使用jQuery中的onchange事件在文本框中未显示字符串值

时间:2018-10-02 10:46:04

标签: c# jquery json model-view-controller

我想显示购买说明。当我输入购买代码onchange事件触发并且json函数调用json函数返回description时,我想在Description文本框中显示描述,但函数返回sting的值,而value不在文本框中显示。任何人都可以在我的代码中指出问题所在。

json函数

public JsonResult PurchaseCode(string glCode)
        {
            try
            {
                return Json(mPurchaseCode(glCode), JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public string mPurchaseCode(string glCode)
        {
            try
            {
                con = new SqlConnection(constring);
                con.Open();
                cmd = new SqlCommand("select gl_desc from ChartofAcct where Comp_Id='" + 1 + "' and Gl_Code='" + glCode + "'", con);
                var varcmd = cmd.ExecuteScalar().ToString();

                con.Close();
                return varcmd;


            }
            catch (Exception)
            {

                throw;
            }

        }

jQuery

<script>

    $('#txtNPurCode').change(function () {
        var purchaseCode = $("#txtNPurCode").val();
        PurchaseCode(purchaseCode);

    });

    function PurchaseCode(purchaseCode) {

        $.ajax({
            url: "/Home/PurchaseCode",
            type: "GET",
            contentType: "application/json;charset=UTF-8",
            data: {
                'glCode': purchaseCode

            },
            dataType: "json",
            success: function (result) {
                $('#txtNPurCodeDescription').val(result.gl_desc);



            },
            error: function (errormessage) {
                alert(errormessage.responseText);
            }
        });
        return false;
    }

</script>

HTML

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

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

                                        <label for="lblRequisitionDatepicker">Purchase 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>

2 个答案:

答案 0 :(得分:0)

您的值中没有json结构。

return varcmd;例如“ ABC”不存在模式

success: function(result) {
    $('#txtNPurCodeDescription').val(result.gl_desc);
},

正确>仅结果。

success: function(result) {
    $('#txtNPurCodeDescription').val(result);
},

答案 1 :(得分:0)

尝试一下:

  $.ajax({
        url: "/Home/PurchaseCode",
        type: "GET",
        contentType: "application/json;charset=UTF-8",
        data: {
            'glCode': purchaseCode

        },
        dataType: "json",
        success: function (result) {
            $('#txtNPurCodeDescription').val(result);



        },
        error: function (errormessage) {
            alert(errormessage.responseText);
        }
    });
    return false;
}