I have a drop-down list where I can choose a location to filter my data.
I have an array that I send to the controller with AJAX. On my local computer, I get the parameter equal to zero if I did not select any of the drop-down list, as expected. However, on my website, it looks like the variable is not null because it is requesting data, so the result is empty.
How can it be? I want the value to be zero, so I can filter the data correctly.
Thanks Stefan
JQuery
$("#main").on("click", "#submit-search-catalog", function () {
var url = "/Home/Catalog";
var locations = $("#County").val();
if (url != "") {
$.post(url, { locations: locations },
function (data) {
if (data) {
$(".catalog-container").html(data);
}
}).fail(function (xhr, status, error) {
alert("Error");
});
}
return false;
});
MVC controller
public async Task<ActionResult> Catalog(int[] locations = null)
{
var companies = await db.Users.Where(u => u.IsVisibleInCatalog.Equals(true)).OrderBy(u => u.CompanyRegistrationDate).ToListAsync();
if (!(locations == null || locations.Length == 0))
{
companies = companies.Where(c => locations.Contains(c.CompanyLocation) || c.CompanyLocation.Equals(1)).ToList();
}
var model = new CatalogViewModel
{
Companies = companies
};
if (Request.IsAjaxRequest())
{
return PartialView("~/Views/Home/_CatalogListPartial.cshtml", companyList);
}
return View(model);
}
- EDIT
I tried this for error testing, and locally it alerts “empty” and serveride it warns “null”:
if (locations == null)
alert("null");
if (locations.length == 0)
alert("empty");