ASP.NET MVC query with optional field and value

trancers21

New Member
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:\[code\]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); }}\[/code\]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:\[code\]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); }}\[/code\]...Except that the \[code\]o["field"]\[/code\] 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!
 
Back
Top