如何在ajax asp.net mvc中填充带有数据(其数据库值)的文本框?

问题描述 投票:1回答:1

我想在文本框中显示我想从数据库中提取的数据。我正在使用ajax。我从一个名为“Urun_gtip”的列中总共有9个不同的值。我想在9个标签中分别显示这些值。如何在视图中显示它们?谢谢。

控制器:

public ActionResult Urun_Ad()
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["APP_LOG_Con"].ConnectionString))
    {
        object ret = null;
        string query = "select distinct a.Urun_Gtip from RelationTBL a select distinct a.Urun_Gtip from RelationTBL a ";

        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.Connection = con;

            con.Open();
            ret = Serialize(cmd.ExecuteReader());
            con.Close();
        }

        return Json(ret, JsonRequestBehavior.AllowGet);
    }
}

视图:

   <div class="modal-body" id="modalData2">
   <div class="row" tabindex="-1">
   <div class=".col-8 .col-sm-6" style="margin-left:25px;">
   <label for="Urun_GID" style="color:#dc3545;">Ürün</label>
   <input class="form-control" name="Urun_Gtip" id="Urun_GID" disabled>
   </div>
  <div class=".col-4 .col-sm-6">
  <label for="Adet" style="color:#dc3545; margin-left:6px;">Adetleri giriniz: 
  </label>
  <input class="form-control" onfocus="this.value=''" name="Adet" id="Adet">
  </div>
   </div>

// I have same 9 row (count of query result is 9)

jQuery的:

$.ajax({
          url: "/Müsavir/Urun_Ad",
          type: "GET",
          success: (data) => {
             // How can I set data to textbox value here?
          }
       });
c# jquery ajax html5 asp.net-mvc-4
1个回答
1
投票

只需解析您获得的json数据并使用循环添加它们:

$.ajax({
          url: "/Müsavir/Urun_Ad",
          type: "GET",
          success: (data) => {
             var json = JSON.parse(data);
             for(var i=0; i<json.length;i++)
                $('#somecontainer').append('<label>' + json.urun_gtip + '</label>);
          }
       });
© www.soinside.com 2019 - 2024. All rights reserved.