So, I'm facing a problem here. I'm building an ASP.NET MVC application, and the final stage is to implement a search page feature that uses multiple filters to perform the search.
I've done a model class called SearchModel that has some properties, among em' is a collection of search filters. The problem is at the search results page. I need to preserve the whole SearchModel as search criteria and additional paging parameters. I'd like to add links to next and previous pages and stuff like that, only available by a GET request. If I enable GET I get a HUGE query string containing the whole model as follows:
Documents/Search?CriteriaFilters[0].Field=OwnerUserName&CriteriaFilters[0].Type=Text&CriteriaFilters[0].Text=albert.sheppard&CriteriaFilters[0].TextMatchMode=Exactly
And that's only using one filter with a small criteria text. Looks bad and nasty when I'm using 10+ filters.
How you pro's handle search and results pages with multiple filters without parsing the whole model to the query string? Query string encryption? If so, how to achieve this?
Thanks.
I don't know about ASP.NET MVC, but it should be done with POST. I understand many of your fields are unused in your example, but they might as well have values, and the size of GET is very limited. Or maybe you can come up with a very compressed way of representing your criteria. But still, POST is a lot better.
I have implemented search criteria URL using MVC as follows:
acer notebook >> Notebook >> 15.6in >> Core 2 Duo
http://myserver/SearchApp/Search/4/acer%20notebook/1/50-97-77868%2c50-111-76631?sort=0&catGrp=50
private
ActionResult SearchAction
(
int siteId,
string query,
int? page,
string options,
SortType sortType,
int catGroupId
)
{
// Search code implementation
}
The above URL and controller translation is as follows:
Search - Action
4 - siteId
acer%20notebook - query
50-97-77868%2c50-111-76631 - options (it is a comma separated list of options), which represents 15.6in >> Core 2 Duo
0 - sortType
50 - catGroup
Hope this will help.
Related
We have 1000 small lookup table editor in database. Each having few rows maximum. Instead of writing 1000 Apis, services, angular proxies we created an Object lookup viewer/editor in grid cell. Small Tables like ProductCodeLookup, AddressType, SupplyCategory, FurnitureType
Also there is API Operator from a webpage, where people can See and Add Rows to the lookup Tables. (see picture link below)
The question came up, how do we dynamically apply Model Validation on the Request Object in API?
Front end validation is on Angular
However, for C#, we want all LookupIds and int category to be 1-100 max. String request members to be 255 characters max. Emails to be in #email regex format etc. We may not know what Object request looks like at runtime, until table is selected.
[HttpPost("[Action]")]
public void AddObject(List<object> addListRequest, string lookupTableName)
{
foreach (var addItem in addListRequest)
{
var addItemCast = addItem.ConvertObjectToTypeWithSerialization(atype);
context.Add(addItemCast);
}
}
This answer for older Net MVC, we are using Net Core 3 API. Dynamically apply validation rules at runtime with ASP.NET MVC 4
Thought there is method to conduct this with new library.
EditorImage
I have an index page where a table is generated with rows of information from the DB.
I have successfully integrated Tom Dykstra's method of adding a search box using this tutorial: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
How would I implement behavior that shows names that already exist as they are being typed in the search box, or automatically update the rows in the table as something is typed into the search box?
I am using MVC 5 with EF6.
Thank you!
I would use the jQuery UI Autocomplete widget with a remote source for the data.
https://jqueryui.com/autocomplete/#remote
For the remote source either write a new action on your controller that returns JSON, or a WebAPI action to do the same, and in there do a query against your database to find matching results for the users query
The best way to do this would be to use AngularJs. We can't give you a full example of this, I can only give you an approach. Angular is very powerful and as well it is client side which speeds up you application very much. See the example shown in the docs of angular.
You can use the filtering on everything (tables, divs, list etc.)
https://docs.angularjs.org/api/ng/filter/filter
Here is an example of how to implement AngularJS in Asp.Net MVC.
http://www.codeproject.com/Articles/806029/Getting-started-with-AngularJS-and-ASP-NET-MVC-Par
Here is also an JSFiddle example on how it can look like:
http://jsfiddle.net/mikeeconroy/QL28C/1/
<tr ng-repeat="product in products | filter:search | orderBy:'name'">
<td>{{product.name}}</td>
<td>{{product.category}}</td>
</tr>
This code snippet would do the filtering for you
I have a conceptual problem.
For an ASP.NET MVC / C# website (although the exact technology may not be that important) I have 2 linked drop downs (html select) with Countries and Cities of a continent.
These are currently kept in 2 database tables and as you imagine the Cities table has round 10 000 records.
The current functionality is:
- initially the country select is populated.
- the user selects a country, an ajax request goes to the server, retrieves the cities for that country_id and populates the second (cities) select.
Sometimes it gets a bit slow as you might imagine, and since these are in the end static values (the lists will not change) what will be the best way to treat this situation?
I would recommend implementing it, using the jquery chosen plugin Chosen
plugin that can be used to filter, results, but since you are getting such a big response you can incorporate a search filter functionality with ajax as shown here, Jquery Ajax-Chosen
I don't know how to ask this, and I don't know what it is called either so I'll just describe what I want to achieve.
In the database, some articles' title originaly has spaces:
my title with spaces
But in the url, spaces are replaced by other characters such as plus sign (+) or underscore (_)
http://www.mydomain.com/mycontroller/myaction/my_title_with_spaces
or
http://www.mydomain.com/mycontroller/myaction/my+title+with+spaces
Now, how do you do that in C#? Or is there any helper in ASP.NET MVC that can do something like that?
Let say we achieved the said URL, is there any risk that two unique titles become the same in the URL? Please consider these titles:
Title's
Titles
after parsing, they became the same
Titles
Titles
This will be a problem when retrieving the article from the database since I'll get two results, one for "Title" and one for "Title's".
I would implement that functionality like this:
1. When creating a new article, generate the URL representation based on the title.
Use a function that converts the title for a suitable representation.
For example, the title "This is an example" might generate something like "This_is_an_example".
This is up to you. You can create a function that parses the title with rules you define, or use an existing one if it suits better your problem.
2. Ensure the URL representation is unique
If it's going to be an ID, it must be unique. So, when creating new articles you must query your database for the resulting URL representation. If you get a result from the database, it means the newly created article generated the same representation as one of the already created articles. Add something to it so it remains unique.
This could be something like "This_is_an_example_2". In this case, we added the "_2" to the end of the generated representation so it differs from the already existing one. Once more, with each change you have to ensure this representation remains unique.
3. Save the created ID in the database, along with the article data
In the database be sure to save the "This_is_an_example" ID and relate it to the article. Maybe even as the table primary key?
4. Query the database for the correct article
Now, about showing a site visitor the correct article:
When a visitor asks for the following resource, for example:
http://www.mydomain.com/mycontroller/myaction/this_is_an_example_2
Extract the URL part that identifies the article, in this case "this_is_an_example_2".
When you have that, you have the identifier of the article in the database. So, you can query the database for the article with the "this_is_an_example_2" ID and show the article's content to the user.
This might involve some URL rewriting. Unfortunately I'm unable to help you with that in asp.NET. Some search on the subject will surely help you.
Web pages have moved to use URLs like:
//weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
i.e. they include the title of the page in the url rather than having some coded id.
I understand that this is useful for SEO, and also for users in finding the correct page where they wish to type in the url.
I would like to follow this approach, but wonder how best to acheive it, and particularly how to deal with duplicates.
Is a database trigger which creates the url based on the title and adds a numeric incremental suffix to any duplicates the best way to go, and if so what would such a trigger look like?
Instead of having an id based on a title they could use id based on both a date and a title (2007/12/03/asp-net-mvc-framework-part-2-url-routing). So if you don't have articles with the same titles in one day (which isn't too severe restriction) duplicates are eliminated.
In Wordpress at least, the "slug" (as they call it) is generated once from the item's title and stored separately in the database. If two "slugs" collide, it appends -1, -2, etc. to the end. I personally prefer if you add an (optional) field to the submission form to allow people to insert their own—it allows people to specify a shorter URL than my-long-article-is-hard-to-type.
You've got to model this concept in your application. URL generation based on title can be automatic, but it can't be invisible. WordPress (and probably other CMS's, too) do a pretty good job of this -- they'll default a URL based on the information you enter, but the "key" part of the URL is visible and editable to the user, and uniqueness is enforced at the appropriate level (globally, per month, per day -- whatever).
Making URL generation completely invisible will lead to confusing errors for the user, I believe.
You could do the same thing that SO does. That is, the slug is only there as GoogleJuice. These two URLs resolve to the same thing:
ASP.Net MVC - route object id == title - how to deal with duplicates?
ASP.Net MVC - route object id == title - how to deal with duplicates?
So, in the example you gave, if the CMS gave each post a unique numeric identifier (which I suppose is quite likely) then you can include it in the URL:
http://weblogs.asp.net/scottgu/archive/2007/12/03/1234/asp-net-mvc-framework-part-2-url-routing
In this example, the symbol 1234 is the post's identifier.