I'm building an app in C# (which is a little rusty) and I'm using an AJAX call to return some data from my database. I want to add some filtering to this by passing an object to the controller.
I've got it working when just one filter is used but I'm struggling when it comes to multiple filters. How do I set this up so I can check to see if that filter is being used and then filter the database on it?
This is my current code:
JS:
dataObj = new Object;
if (FormData) {
$.each(FormData, function (index, item) {
dataObj[item.name] = item.value;
});
};
$.ajax({
url: '@Url.Action("GetCompanies")',
data: dataObj,
type: 'GET',
dataType: 'json',
success: function (result) {
//Do stuff with data
}
});
Controller:
public JsonResult GetCompanies(SearchFilter SearchFilters)
{
if (!String.IsNullOrEmpty(SearchFilters.CustomerType) && SearchFilters.CustomerType != "All")
{
var data = _db.Customers.Where(a => a.Type.Contains(SearchFilters.CustomerType)).ToList();
var jsonData = Json(data, JsonRequestBehavior.AllowGet);
return jsonData;
}
else
{
var data = _db.Customers.ToList();
var jsonData = Json(data, JsonRequestBehavior.AllowGet);
return jsonData;
}
}
Thanks in advance!