How to use a c# net combobox filter using SQL Server?

Asked

Viewed 741 times

0

I need to improve on a project I’m doing in MVC, with SQL and I’ve done a combobox that takes the data from the database, but I need to create a page to filter data from the database.

Ex: I have a text field and a combobox with database data types, I don’t just want a data, for example if I just want to filter by a type, or by name. There’s a way I can do this without doing several combobox?

  • Hello. It’s not very clear what your need is. Could you explain better what you need to do?

  • @Mariane Ribeiro. Hi, if I understand correctly, you have a query and from it you want to add other filter options in this same query.

  • @Luizvichiatto exact this!! I do not want to filter through a specific object of my database, would have how? I thank you already!!

  • @Joelrodrigues actually I’m working with other layers within the mvc, With Repository, Dao and the business layer, already understood the concept of MVC there joined more layers and as I am at the beginning gave a complicated, hehe... But so it could be like this, not to complicate, I wanted to know how to make a page to filter activities... Thank you for your attention!!

  • Let me get this straight. Do you want to have a multi-column combobox of your table and be able to add multiple filters to generate a dynamic query? For example, the user selects NAME and adds a value, then selects CATEGORY and enters another value? In the end it would generate a select of type WHERE CAMPO1 = 'x' AND CAMPO2 = 'y' ...?

  • @Joelrodrigues exactly this! summed up exactly what I need! :)

Show 1 more comment

1 answer

0


Hello. As I understand it, you can use jQuery to add new "filter lines" in your form (using the append function, for example) and submit this data as string arrays for your action. In the action you go through the arrays with column names and values (maybe also the type, like CONTAINS, DOES NOT CONTAIN, etc.) and generate your query.

To generate new lines, there are some ways:

  • Create a new div; create the internal elements and insert them into the div; insert the div into the form.
  • Add the div already with the internal elements by inserting direct HTML code ($("#meuform").append("<div><select...")).
  • Create an action that returns a view with only one filter line, containing select, input, button, etc. Load and load this view via Ajax by inserting it into the form.
  • Create a "model" div, containing the filter elements (select, input, etc.), leave it with display:None and to insert a new line in the form, copy the contents of that div (using the html() function, for example).

With these lines created in the form, you submit to your action and there generates the query.

  • thank you so much! It helped a lot!! Vlw even by the knowledge ;) A good day!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.