Implementing filters for a SQL table display on a view in ASP.NET MVC (C#) and LINQ-to-SQL? -


A follow-up from

I have changed my controller and routed around so that the sort value is sorted now = Although I also want to apply the ability to filter the table based on a selected group of values ​​for users:

Technician: Tech 1, Tech2, Tech3 etc. Category: Category 1, Category 2, Category 3, etc. Priority: Priority 1, Priority 2, Priority 3, etc.

I'm only using a table at this time, so the easiest way to filter the list of available categories is to have different values ​​in the table. They are all input via HTML, so I have control over what control I have. I do not want to divide them all into different tables, so far as I can with a table, but if it is a scenario that breaks a one-table-model then ok, I adjust I can do

To illustrate the case of use, perhaps the best one to show a quick screenshot of the scene in question is:

I implemented a system like this To which a user can show issues where the priority was "Investigative".

I am considering the idea of ​​how to apply both sides of the front and rear. I have tried to find a site that is similar to a table of data but I can not think of one on top of my head, though I am sure it is a feature which has been applied a hundred times earlier!

Can someone recommend a good way of doing this?

server level

Let's start with some pseudo-d.

  Public Action Result Open (string string, string category, string preference) {// generate query // filter stuff // command / sort stuff}  
< P> It is necessary to correct the order of operations, here. Some IQueryable providers ( I see you, unit framework ) will ignore order to arrive before filtering. So start by creating an unwanted, unfiltered query, such as with your last question, then attach the filtering, and finally attach the sequence.

Note that I have argued for your action for every possible filter case. One way to do this is to look at the other way to sort out the query string parameters (inside the request) for any type of treatment, to filter again on anything else, as a potential filter "sort "Not to say you should choose that your app fits best.

Now, how do you actually implement filtering? What Mark shows is one way that it really works well, if you only want to filter certain cases. Obviously, I will put it in a supportive system, rather than in action itself. Chances are you need to reuse this assistant and other tasks. However, if you expect to filter in a line, then you can offer filtering in one line, another way is to use the Microsoft Dynamic Linq Library, which you can get from Codepex. This allows you to create a LINQ. Where strings are used instead of lambda expressions.

Browser level

Now that your application can control it, you need a method to create that link that looks like:

If you have a menu of filter options, for example, each menu item will correspond to a link with different query string parameters.

This is a bit tricky because you probably want to preserve the current query string parameter while adding a new one. For example, if a user ID resolves open issues, and only chooses to filter the checking priority issues, you still want to preserve the sort, so all the links in the menu are currently The query string parameters for the displayed page must be included in the query string parameter for the menu item.

Therefore, the general approach that we do for each item (or who link to your building) in the "Filtering Menu":

  1. Query Create a RootviewView of string parameters
  2. Pass all query string parameters in Current dictionary in the dictionary.
  3. Add an additional query string parameter for the current menu item.
  4. Generate the final URL using Html.RouteLink and the dictionary we created.

Comments