What I'm trying to do is provide a generic search capability on a table. So the user is presented with all data in a table, they enter some text to filter on, and voila, the table is now filtered on all results that match that entry.
I have this working with a single field:
public ActionResult Index(string user_name)
{
var dataContext = new PurchaseOrderDataContext();
var orders = from o in dataContext.purchase_orders
where o.approved_by.display_name.StartsWith(user_name ?? "")
select o;
if (Request.IsAjaxRequest())
{
return PartialView("list", orders);
}
else
{
return View(orders);
}
}
But what I'm looking for is the ability for them to have a dropdown that allows them to pass in the field that they want to filter on. It would like something like this:
public ActionResult Index(string field, string query_value)
{
var dataContext = new PurchaseOrderDataContext();
var orders = from o in dataContext.purchase_orders
where o["field"].StartsWith(query_value ?? "")
select o;
if (Request.IsAjaxRequest())
{
return PartialView("list", orders);
}
else
{
return View(orders);
}
}
...Except that the o["field"]
is just my silly guess at syntax (which doesn't work).
I guess I'm just confused because although I'm using this nice class-ified data model, I sometimes want to explicitly refer to the columns by name.
How would I go about doing this?
My apologies if this is obvious...
Edit: I think I'm going to leave this open for now to see if there's a neater, less hack-ish feeling solution than suggested below. Thanks!