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)