I am working on an application using asp.net mvc 4
. Here I want to use autocomplete extender using JQuery
where i want to populate all location of that CityID
which is stored in session.
Here is the function for creating the session:
public string Create(string City)
{
try
{
//HttpSessionStateBase Session["c"]=City;
//HttpContext.Current.Session["City"] = City;
System.Web.HttpContext.Current.Session["City"] = City;
long CityID = Convert.ToInt64(System.Web.HttpContext.Current.Session["City"].ToString());
return City;
}
catch (Exception ex)
{
throw (ex);
}
}
This function is called when user select any city from the City dropdownlist
.
My JQuery
calling for autocomplete extender is:
<script type="text/javascript">
var url = '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "ProductApi" })';
$('#txtLocation').autocomplete({
source: function (request, response) {
alert(url);
$.ajax({
url: url,
data: { query: request.term },
dataType: 'json',
type: 'GET',
success: function (data) {
response($.map(data, function (item) {
return {
label: item.Name
//value: item.ID
}
}));
}
})
},
select: function (event, ui) {
$('#txtLocation').val(ui.item.label);
//$('#ID').val(ui.item.value);
return false;
},
minLength: 1
});
My api controller is :
public class ProductApiController : ApiController
{
SqlConnection cnn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["SQLCONN"].ToString());
[HttpGet]
public IEnumerable<Search> GetProducts(string query = "")
{
cnn.Open();
//string gid = GETSession("t");
long CityID = Convert.ToInt64(System.Web.HttpContext.Current.Session["City"].ToString());
SqlCommand cmd = new SqlCommand("Check_PrefixText", cnn);
cmd.Parameters.AddWithValue("City", CityID);
cmd.Parameters.AddWithValue("@Prefix", query);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
//var result = IEnumerable<City>(query);
Search obj = new Search();
cnn.Close();
return dt.AsEnumerable().Select(row =>
{
return new Search
{
Name = Convert.ToString(row["Name"]),
};
});
}
}
In Global.asax
file i have written 2 methods:
protected void Application_PostAuthorizeRequest()
{
if (IsWebApiRequest())
{
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}
}
private bool IsWebApiRequest()
{
return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith(WebApiConfig.UrlPrefixRelative);
}
In webApiConfig
Class i have written as follows:
public static string UrlPrefix { get { return "api"; } }
public static string UrlPrefixRelative { get { return "~/api"; } }
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: WebApiConfig.UrlPrefix + "/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
But still the value of session["City"]
is coming null in ApiController
while there there is value stored showing in session["City"]
.