简体   繁体   English

根据下拉列表选择值从数据库中获取下拉选择值到两个文本框,IDIN MVC4 jQuery

[英]Get dropdown selected value to two textbox from database based on dropdown list selected value idin mvc4 jquery

I'm using JSON for drop down list select value based on id have to insert in 2 textboxes from database where I use the code in jQuery 我正在使用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)
        }
    });
});

Here I have got one textbox value. 在这里,我有一个文本框值。 How can I get another textbox value from the database? 如何从数据库中获取另一个文本框值?

My controller code is: 我的控制器代码是:

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

You should return a JSON object like 您应该返回一个JSON对象,例如

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

Which can be used like 可以像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM