I\'m building an MVC app in which the user will have the possibilities of using a lot of filters to get exactly what he wants.
Here\'s an overview of those filters based
Ho do you normally face this in JavaScript ? You would create an option
object, like
var op = {
cardName : "..",
cardType : "" ,
...
}
and pass it to a function. Correct ?
The same do in C#
too.
Define a, say, Conditions
class, like
public class Conditions {
public string CardName {get;set;}
public string CardType {get;set;}
..
}
and pass it to your function, which controls the values of the properties of that type to act accordingly after.
Here's a way you could handle your problem.
First, create a ViewModel with your search parameters.
public class FilterViewModel
{
public string ObjName { get; set; }
public string ObjType { get; set; }
public string OtherType { get; set; }
}
In your controller, pass this to the view.
public virtual ActionResult SearchResults()
{
return View(new FilterViewModel());
}
In your SearchResults.cshtml you need to encode the model to a json and then use an ajax call to filter your results. Assumming there's a button with btnFilterResults id. This is going to load the results from a partial view (that should be your table) using ajax.
<script type="text/javascript">
var dataViewModel = @Html.Raw(Json.Encode(Model));
$("#btnFilterResults").click(function () {
dataViewModel.ObjName = $("#objName").val(); //Make sure the ids corresponde to your html.
dataViewModel.ObjType = $("#objType").val();
dataViewModel.OtherType = $("#otherType").val();
$.ajax({
url: "@Url.Action(MVC.ControllerName.GetSearchResults())",
data: { viewModel: JSON.stringify(dataViewModel) },
success: function(data) {
$("#tableDiv").empty(); //
$("#tableDiv").html(data);
}
});
});
</script>
Finally, in your controller you can get the results:
[HttpGet]
public virtual ActionResult LoadTableReporteGeneral(string viewModel)
{
var filterOptions = JsonConvert.DeserializeObject<FilterViewModel>(viewModel);
var query = from t in db.YourTableName
select t;
//This returns an IQueryable with all of your data, no filtering yet.
//Start filtering.
if(!String.IsNullOrEmpty(filterOptions.ObjName))
{
query = query.Where(x => x.ObjName == filterOptions.ObjName);
}
if(!String.IsNullOrEmpty(filterOptions.ObjType ))
{
query = query.Where(x => x.ObjType == filterOptions.ObjType );
}
//and so on.
//Finally return a partial view with your table.
return PartialView(MVC.Shared.Views._YourTableName, query);
}
Note: I'm using T4MVC and Json.Net packages.
Hope this helps.
Why do you not use ModelBinder
. I think it's very useful with your case. Please see below:
_ I have a Model
:
public class Employee
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public Address HomeAddress { get; set; }
}
and ModelBinder
for Employee
:
public class EmployeeBinder : IModelBinder {
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
var emp = new Employee {
Id = controllerContext.HttpContext.Request.Form["Id"],
FirstName = controllerContext.HttpContext.Request.Form["FirstName"],
LastName = controllerContext.HttpContext.Request.Form["LastName"],
BirthDate = new DateTime(int.Parse(controllerContext.HttpContext.Request.Form["year"]),
int.Parse(controllerContext.HttpContext.Request.Form["month"]),
int.Parse(controllerContext.HttpContext.Request.Form["day"]))
};
return emp;
}
}
Then Controller
:
public ActionResult Example(Employee emp) {
return View(emp);
}
URL: http://localhost:1034/Home/Example?Id=1&LastName=lazycatit