I am trying to get the AutoComplete control to work with a WCF method that takes in parameters, I did get the autocomplete control to work with a wcf method that does not have any parameters, but when I try to pass in parameters it won't work. My code is below:
[ServiceContract]
public interface IShippers
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
IEnumerable<Company> QueryDatabase(string columns, string table, string whereClause);
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
IEnumerable<Company> GetShippers();
}
[DataContract]
public class Company
{
public Company(string id, string name)
{
Id = id;
Name = name;
}
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Shippers : IShippers
{
public IEnumerable<Company> QueryDatabase(string columns, string table, string whereClause)
{
List<Company> shippers = new List<Company>();
try
{
var sql = string.Concat("select ", columns, " from ", table, " where ", whereClause);
var drSql = ExecuteSql(sql);
while (drSql.Read())
{
shippers.Add(new Company(drSql.GetString(1), drSql.GetString(0)));
}
drSql.Close();
return shippers;
}
catch (Exception ex)
{
var message = ex.Message;
return null;
}
}
private SqlDataReader ExecuteSql(string sql, bool useForXml = false)
{
const string connectionString = "Server=CLE-BWRENN;Database=TrucoCompany;Trusted_Connection=True;";
if (useForXml)
sql = string.Concat(sql, "FOR XML");
SqlConnection cnSql = new SqlConnection(connectionString);
cnSql.Open();
SqlCommand cmSql = new SqlCommand(sql, cnSql);
SqlDataReader drSql = cmSql.ExecuteReader(CommandBehavior.CloseConnection);
return drSql;
}
public IEnumerable<Company> GetShippers()
{
List<Company> shippers = new List<Company>();
var drSql = ExecuteSql("select cmp_name, cmp_id from company where cmp_shipper = 'y'");
while (drSql.Read())
{
shippers.Add(new Company(drSql.GetString(1), drSql.GetString(0)));
}
drSql.Close();
return shippers;
}
}
<div id="AutoCompleteTest" class="k-content">
<div id="Shipper">
<input id="input" />
<div class="hint">type shipper</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
//$.ajax({
// type: "post",
// url: "http://localhost/WCFTest/Service1.svc/QueryDatabase",
// data: '{"columns":"cmp_name,cmp_id", "table":"company", "whereClause":"cmp_shipper = \'y\'"}',
// dataType: "json",
// contentType: "application/json; charset=utf-8",
// success: function(result) {
// debugger;
// },
// error: function (result) {
// debugger;
// }
//});
$("#input").kendoAutoComplete({
dataTextField: "Name",
template: "<span><b>${ data.Name }</b></span> <span>${ data.Id }</span>",
dataSource: {
minLength: 1,
type: "json",
schema: {
data: "d",
},
transport: {
read: {
url: "http://localhost/WCFTest/Service1.svc/QueryDatabase",
dataType: "json",
type: "POST"
},
parameterMap: function () {
var data = {
columns: "cmp_name,cmp_id",
table: "company",
whereClause: "cmp_invcopies = 1"
};
return JSON.stringify(data);
},
}
},
filter: "startswith",
placeholder: "Select ...",
separator: ","
});
});
</script>
<style scoped="scoped">
.k-autocomplete {
width: 500px;
}
#AutoCompleteTest .k-autocomplete {
width: 250px;
}
</style>
</div>
[ServiceContract]
public interface IShippers
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
IEnumerable<Company> QueryDatabase(string columns, string table, string whereClause);
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
IEnumerable<Company> GetShippers();
}
[DataContract]
public class Company
{
public Company(string id, string name)
{
Id = id;
Name = name;
}
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Shippers : IShippers
{
public IEnumerable<Company> QueryDatabase(string columns, string table, string whereClause)
{
List<Company> shippers = new List<Company>();
try
{
var sql = string.Concat("select ", columns, " from ", table, " where ", whereClause);
var drSql = ExecuteSql(sql);
while (drSql.Read())
{
shippers.Add(new Company(drSql.GetString(1), drSql.GetString(0)));
}
drSql.Close();
return shippers;
}
catch (Exception ex)
{
var message = ex.Message;
return null;
}
}
private SqlDataReader ExecuteSql(string sql, bool useForXml = false)
{
const string connectionString = "Server=CLE-BWRENN;Database=TrucoCompany;Trusted_Connection=True;";
if (useForXml)
sql = string.Concat(sql, "FOR XML");
SqlConnection cnSql = new SqlConnection(connectionString);
cnSql.Open();
SqlCommand cmSql = new SqlCommand(sql, cnSql);
SqlDataReader drSql = cmSql.ExecuteReader(CommandBehavior.CloseConnection);
return drSql;
}
public IEnumerable<Company> GetShippers()
{
List<Company> shippers = new List<Company>();
var drSql = ExecuteSql("select cmp_name, cmp_id from company where cmp_shipper = 'y'");
while (drSql.Read())
{
shippers.Add(new Company(drSql.GetString(1), drSql.GetString(0)));
}
drSql.Close();
return shippers;
}
}
<div id="AutoCompleteTest" class="k-content">
<div id="Shipper">
<input id="input" />
<div class="hint">type shipper</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
//$.ajax({
// type: "post",
// url: "http://localhost/WCFTest/Service1.svc/QueryDatabase",
// data: '{"columns":"cmp_name,cmp_id", "table":"company", "whereClause":"cmp_shipper = \'y\'"}',
// dataType: "json",
// contentType: "application/json; charset=utf-8",
// success: function(result) {
// debugger;
// },
// error: function (result) {
// debugger;
// }
//});
$("#input").kendoAutoComplete({
dataTextField: "Name",
template: "<span><b>${ data.Name }</b></span> <span>${ data.Id }</span>",
dataSource: {
minLength: 1,
type: "json",
schema: {
data: "d",
},
transport: {
read: {
url: "http://localhost/WCFTest/Service1.svc/QueryDatabase",
dataType: "json",
type: "POST"
},
parameterMap: function () {
var data = {
columns: "cmp_name,cmp_id",
table: "company",
whereClause: "cmp_invcopies = 1"
};
return JSON.stringify(data);
},
}
},
filter: "startswith",
placeholder: "Select ...",
separator: ","
});
});
</script>
<style scoped="scoped">
.k-autocomplete {
width: 500px;
}
#AutoCompleteTest .k-autocomplete {
width: 250px;
}
</style>
</div>