1

I want to show a data in a textbox that I want to extract from the database. I'm using ajax for this. I have a total of 9 different values from a column called "Urun_gtip". And I want to show these values separately in 9 labels. How can I show them in the view? Thanks.

Controller:

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

View:

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

1 Answer 1

1

Just parse the json data you are getting and add them using a loop:

$.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>);
          }
       });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.