【问题标题】:Get dropdown selected value to two textbox from database based on dropdown list selected value idin mvc4 jquery基于下拉列表选择值idin mvc4 jquery从数据库中获取下拉选择值到两个文本框
【发布时间】:2014-12-23 09:38:38
【问题描述】:

我使用 JSON 作为下拉列表选择值,基于 id 必须从数据库中插入 2 个文本框 我在 jQuery 中使用代码的地方

$("#Lt").change(function () {
    $.ajax({
        url: '@Url.Action("code", "Home")',  
        type: "POST",
        data: JSON.stringify({ id: $("#Lt").val() }),
        dataType: "json",
        async: false,
        contentType: 'application/json,charset=utf-8',
        success: function (data) {
            $("#AgreementSeries").val(data)
        }
    });
});

这里我有一个文本框值。如何从数据库中获取另一个文本框值?

我的控制器代码是:

public JsonResult code(string id)
{
    string no;
    string series;
    int _id = Convert.ToInt32(id);
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd = new SqlCommand("SELECT top(1) Agreementseries, num from loan where id = @ID", con);
    cmd.Parameters.AddWithValue("@ID", _id);
    cmd.CommandType = CommandType.Text;
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);
    series = ds.Tables[0].Rows[0]["Agreementseries"].ToString();
    no = ds.Tables[0].Rows[0]["num"].ToString();
    return Json(series, no, JsonRequestBehavior.AllowGet);
}

【问题讨论】:

    标签: javascript c# jquery asp.net-mvc-4


    【解决方案1】:

    你应该返回一个类似的 JSON 对象

    return Json(new {
                    series =series, 
                    no = no
                }, JsonRequestBehavior.AllowGet);
    

    可以像这样使用

    success: function (data) {
        $("#AgreementSeries").val(data.series);
        //Use data.no as per your requirement
    }
    

    【讨论】:

    • 我得到了你所说的非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    相关资源
    最近更新 更多