in Education by
I have an ASP.NET MVC website. In my backend I have a table called People with the following columns: ID Name Age Location ... (a number of other cols) I have a generic web page that uses model binding to query this data. Here is my controller action: public ActionResult GetData(FilterParams filterParams) { return View(_dataAccess.Retrieve(filterParams.Name, filterParams.Age, filterParams.location, . . .) } which maps onto something like this: http://www.mysite.com/MyController/GetData?Name=Bill .. . The dataAccess layer simply checks each parameter to see if its populated to add to the db where clause. This works great. I now want to be able to store a user's filtered queries and I am trying to figure out the best way to store a specific filter. As some of the filters only have one param in the queryString while others have 10+ fields in the filter I can't figure out the most elegant way to storing this query "filter info" into my database. Options I can think of are: Have a complete replicate of the table (with some extra cols) but call it PeopleFilterQueries and populate in each record a FilterName and put the value of the filter in each of field (Name, etc) Store a table with just FilterName and a string where I store the actual querystring Name=Bill&Location=NewYork. This way I won't have to keep adding new columns if the filters change or grow. What is the best practice for this situation? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
If the purpose is to save a list of recently used filters, I would serialise the complete FilterParams object into an XML field/column after the model binding has occurred. By saving it into a XML field you're also giving yourself the flexibility to use XQuery and DML should the need arise at a later date for more performance focused querying of the information. public ActionResult GetData(FilterParams filterParams) { // Peform action to get the information from your data access layer here var someData = _dataAccess.Retrieve(filterParams.Name, filterParams.Age, filterParams.location, . . .); // Save the search that was used to retrieve later here _dataAccess.SaveFilter(filterParams); return View(someData); } And then in your DataAccess Class you'll want to have two Methods, one for saving and one for retrieving the filters: public void SaveFilter(FilterParams filterParams){ var ser = new System.Xml.Serialization.XmlSerializer(typeof(FilterParams)); using (var stream = new StringWriter()) { // serialise to the stream ser.Serialize(stream, filterParams); } //Add new database entry here, with a serialised string created from the FilterParams obj someDBClass.SaveFilterToDB(stream.ToString()); } Then when you want to retrieve a saved filter, perhaps by Id: public FilterParams GetFilter(int filterId){ //Get the XML blob from your database as a string string filter = someDBClass.GetFilterAsString(filterId); var ser = new System.Xml.Serialization.XmlSerializer(typeof(FilterParams)); using (var sr = new StringReader(filterParams)) { return (FilterParams)ser.Deserialize(sr); } } Remember that your FilterParams class must have a default (i.e. parameterless) constructor, and you can use the [XmlIgnore] attribute to prevent properties from being serialised into the database should you wish. public class FilterParams{ public string Name {get;set;} public string Age {get;set;} [XmlIgnore] public string PropertyYouDontWantToSerialise {get;set;} } Note: The SaveFilter returns Void and there is no error handling for brevity.

Related questions

0 votes
    I have already tried to make a login form and I also want to display the user login profile that ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have already tried to make a login form and I also want to display the user login profile that ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I'm looking to build an reusable control or custom helper for my MVC project. I'm sure there is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm looking to build an reusable control or custom helper for my MVC project. I'm sure there is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm looking to build an reusable control or custom helper for my MVC project. I'm sure there is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Details: We are using Keycloak authentication server with Asp.Net WebAPI. Now I need to get the Keycloak ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have user control named DateTimeUC which has two textboxes on its markup: I am dynamically creating this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I have user control named DateTimeUC which has two textboxes on its markup: I am dynamically creating this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I have user control named DateTimeUC which has two textboxes on its markup: I am dynamically creating this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I write the following code to access the page but this is not working for me if (User.Identity. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I write the following code to access the page but this is not working for me if (User.Identity. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I have a scenario where i want to apply cache on a user control in asp.net mvc 2. I have ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I have an Azure table datastore and here's my model for one class: public class Product : ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I have some website which requires a logon and shows sensitive information. The person goes to the page, is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    Is there a way to comment out markup in an .ASPX page so that it isn't delivered to the client? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
...