0

C#,ASP.NET MVC,Ajax:-I have a form page where there is a drop down list showing customers name from database table customers.Based on the customer selection by passing customer id to database table I want to fetch his email id and contact number and display it in form read only text fields.My problem is controller code is not working.In other words i am not able to check whether id is reaching my controller function to fetch data and the query is executing or not.I am getting error function data on form page

My jquery code is as below

CustomerList1 is the id for drop down box

var cusKey to hold customer id

GetCustmrDetails is my controller function to fetch data based on id

$("#CustomerList1").change(function (event) {
        var cusKey = $("#CustomerList1").val();
        alert("customerKey:" + cusKey);
        $.ajax({
              type: 'GET',
              url: "/CustomerController/GetCustmrDetails/",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              data: JSON.stringify({ "id": cusKey }),
            success: function (data) {
                alert("available");
                $("#EMailAddress").val(data.EMailAddress);
                $('#Phone').val(data.Phone);
            },
            error: function () {
                alert("error");
            }
        });
    });

My controller code is as below

[HttpGet]
    public ActionResult GetCustmrDetails(int id)
    {

        List<CustomerModel> customer = new List<CustomerModel>();
        string query = string.Format("Select EMailAddress,Phone From 
        OC_Customer where CustomerKey="+id);
        SqlConnection connection = new SqlConnection(connectionString);
        {
            using (SqlCommand cmd = new SqlCommand(query, connection))
            {
                connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    customer.Add(new CustomerModel{
                        EMailAddress = reader["EMailAddress"].ToString(),
                        Phone = reader["Phone"].ToString()
                    });
                }
            }
            return Json(customer, JsonRequestBehavior.AllowGet);
        }
}

I want email id and contact number to display on form page(cshtml)

7
  • change only url: "/CustomerController/GetCustmrDetails?id="+cusKey Commented Jan 21, 2019 at 6:12
  • url changed but still not working error function is executing Commented Jan 21, 2019 at 6:20
  • what error show in console Commented Jan 21, 2019 at 6:21
  • can anyone provide correct code to be placed in GetCustmrDetails function Commented Jan 21, 2019 at 6:22
  • this alert alert("customerKey:" + cusKey); is occure or not Commented Jan 21, 2019 at 6:25

1 Answer 1

2

please use this one

  $("#CustomerList1").change(function (event) {
    var cusKey = $(this).val();
    alert("customerKey:" + cusKey);
    $.ajax({
        type: 'GET',
        url: "/Customer/GetCustmrDetails?id="+cusKey,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{}",
        success: function (data) {
            alert("available");
            $("#EMailAddress").val(data[0].EMailAddress); 
            $('#Phone').val(data[0].Phone);
        },
        error: function () {
            alert("error");
        }
    });
});
Sign up to request clarification or add additional context in comments.

7 Comments

still not fetching data from database table and not displaying the email and contact number in form
now hitting to method GetCustmrDetails or not, check by puting debugger
yes hitting to method.thanks,but how to put debugger?
now alert available is also working but this code to display details not working $("#EMailAddress").val(data.EMailAddress); $('#Phone').val(data.Phone);
i got the code to display in correct format it's $("#EMailAddress").val(data[0].EMailAddress); $('#Phone').val(data[0].Phone);
|

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.