Implementing refined search - ASP.NET MVC - asp.net-mvc

In my ASP.NET MVC application I have a view that displays a list of Products in the system. I would like to implement an option for users to filter the list of Products by selecting parametes, similar to the way it's done on www.codeplex.com. I would like to know how you would go about doing this in the most efficient and simple way? Any links to tutorials or guides are appreciated.

In our application we load up a list of all of the products into the web page, and use the Quicksearch jQuery plugin to filter the list. This allows the user to enter a word or two into a textbox, which collapses the list to only those entries matching what the user typed.

Basically, for a search of this type (server-side), you need:
Fields in a <form> for the user to fill out to perform the search request.
A button to post the form fields to your controller method
A repository for the Linq queries that will return the proper records.
A Method in the repository that accepts the parameters you have captured, and executes a linq query returning your filtered result, using Where clauses to filter the returned records.
The result of the query gets returned to the view for display.
If you need dynamic capabilities (i.e. the user may omit one or more parameters, and you need the flexibility to specify those parameters in the Linq query at runtime), then have a look at Dynamic Linq.

Related

Best Practice, render same partial multiple times, similar id issue

I have a master-detail-master-detail (order header-> order details-> work order header->Work order details) relationship that I want to show on a single page. I have a standard form to start with the order header, then a series of kendo ui tabs, one for each order detail. Within the tab, I display a partial view showing a nested master detail relationship showing the Work Order Header (form) and Work order Details (grid).
The problem with this is that since the partial is being generated multiple times on the same page, you have non-unique element ids.
All in all, it works, except that it violates non-unique Id rules and since I have some kendo widgets in the partial, they don't work because of this.
So just wondering what is the best way to handle this? I suppose you could put a suffix on each Id, and then capture the form submit and strip the suffix off before the form is submitted to the controller...
Are there any other ways?

Messagebox from MVC cntroller action

I am new to the MVC so excuse me for asking this basic question. My requirement is simple, I have got a view where user can provide the search criteria and then clicks the 'Search' button. If no matching records found for the entered search criteria then I need to show a message box to the user and stay at the same view.
How to do this?
Your Search action method can pass an IEnumerable<T> into the corresponding view. If the model's Count() method returns zero, you can display a message box; otherwise, you display the search results.
Sounds like AJAX is the way to go, but without specifics regarding language or framework it's hard to say more.

MVC Models for complex page

I'm working on an ASP.Net MVC project. My index page will be similar to facebook's which means that the user can write a message but also sees the messages of his/her friends and a list of his friends is shown too. That means that there are two outputs and one input.
How should my Models for this page look like? Is it a good idea to have one IndexModel containing a list of all messages (List), a list of all friends (List), and an InputMessage class?
Or should I write one Model for each of them and put them together within a ViewModel?
Thanks
Your best bet is actually to split out either the friends list, messages list or both into their own partial views. Then if you don't want to have one controller action generate data for them, you can create actions for each of them and use Html.RenderAction to show them.
http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.renderaction.aspx
If I am correct then your webpage will have static(list of friends) as well as dynamic(list of messages) content. I would suggest you to have a strongly typed view with with your model containing all the static content including the list of friends e.g. IEnumerable.
For messages create partail view using jQuery-template feature. Define the template as on how to display the messages, bind the template with raw json data(which will basically contain your messages) and embed this partial view in you strongly typed view.
Partial views can be resused so tomorrow you can use the same view to show messages else where in application.
For more on how to design using jQuery template : https://github.com/nje/jquery-tmpl/wiki/List-of-jQuery-tmpl-articles-and-tutorials
Friends and Messages are two different concerns so they got to be in different ActionResults, no matter how you plan to display them later on (using templating or something else)

MVC Paging and Sorting Patterns: How to Page or Sort Re-Using Form Criteria

What is the best ASP.NET MVC pattern for paging data when the data is filtered by form criteria?
This question is similar to: Preserve data in .net mvc but surely there is a better answer?
Currently, when I click the search button this action is called:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Search(MemberSearchForm formSp, int? pageIndex, string sortExpression)
{}
That is perfect for the initial display of the results in the table.
But I want to have page number links or sort expression links re-post the current form data (the user entered it the first time - persisted because it is returned as viewdata), along with extra route params 'pageIndex' or 'sortExpression',
Can an ActionLink or RouteLink (which I would use for page numbers) post the form to the url they specify?
<%= Html.RouteLink("page 2", "MemberSearch", new { pageIndex = 1 })%>
At the moment they just do a basic redirect and do not post the form values so the search page loads fresh.
In regular old web forms I used to persist the search params (MemberSearchForm) in the ViewState and have a GridView paging or sorting event reuse it.
One possible solution is to attach a javascript click handler to the pager links that will submit the form by updating a hidden field containing the page number. This way you will get all the search criteria in the controller action.
Another possibility is to transform those pager links into submit buttons and place them inside the form.
A third possibility is to use the Session to persist search criteria.
You could perform a GET instead of a POST. if your request is to return search results, a GET might make more sense anyway. The benifit would be that all of your search fields are encoded into the URL. So, when you perform a page or sort on th exisiting URL, your data is perserved.
I have an example that using the MvcContrib Grid and Pager here:
http://weblogs.asp.net/rajbk/archive/2010/05/08/asp-net-mvc-paging-sorting-filtering-using-the-mvccontrib-grid-and-pager.aspx

Persisting data from multiple forms in ASP.NET MVC

I have two forms on one page: a results form and a search form. The search form uses a partial view because it is displayed on several different pages. I want to be able to persist the data in the search form regardles of which button on which form the user clicks. The problem is that when the user clicks on a link or button from the results form, only the form values from the results form are posted, the values from the search form are not included. How can I maintain the values in the search form even when it is not the form that is submitted? I do not want to use any type of session state to maintain the form and I dont want to write the search values in hidden fields in the results form. I just want to be able to post them with the form values of the results form so that the users search criteria can be maintained accross any page that displays the search partial view. What am I missing?
The first thought that occured to me is to remove the form wrapping the search control and just let it be rendered into the form with the results data. I worry here about naming conflicts. What happens when the search from has a control with the same name as the results form, wouldn't this cause a naming conflict? I suppose that this could just be managed manually to ensure that there are unique names whenever rendering partial views into other views, perhaps even going so far as to prefix values with the partial view name, but that reminds me of the ugliness that is INamingContainer in web forms - plus makes for cumbersome field names in your model.
Is there some sort of elegant solution that will allow a form to persist state that I am missing? Thanks!
Normally, I persist the search criteria on the server side when the search is performed. If the user changes the search criteria after performing the search, then posts the form any changes are, of course, lost but that's arguably correct behavior since the search wasn't invoked. This is true whether the search is performed from a full post or via ajax. Handling it this way keeps the actions cleaner, I think as you don't need to handle the search data in the other actions.
If you absolutely need to have the search parameters included, you could consider having the second form post via javascript, pick up the search field values dynamically and add them to the second form (as hidden fields) prior to posting the second form. You wouldn't have to maintain the values in two places in synchronization, but you would have to copy them to the second form before posting.
At the moment i got it like this:
Forms which has search box, posts query (and additional data if needed) to search controller which then renders search view. Search view is made from search box and search results partial views. During this - search box form is reconstructed by posted data.
If i need search results form to perform another search request (for example, with specified page index), it goes through ajax, which posts search box form + page index from search results form. Take a look here for ideas (update that JS method with targetId parameter for updating specified div/form and post additional data if needed here like this:
form.serialize()+"&pageIndex=5"
In short: if you need to maintain state of form + update another in one page - consider using partial updates, otherwise you will end up inventing ViewState 2.0.
One caveat with this way - it's tricky to make search box contain something what is related with search results (i.e. - total count of found items). Before i managed to handle this, our designer did that - i just need to add div with appropriate class name ("sbsubst" or something) and it looks that it's inside search box. :)
When you have few forms at your page each form sends only its own data. In WebForms you had only one form (at least server-side) and each control was included into this form. In ASP.NET MVC you can use the same scenario and I'm afraid you will have to if you want to have the described behavior. Don't forget - partial forms don't have to be real forms. Moreover, RenderPartial is mostly used for "control-like" layout creation.
As for the second part of your question I would suggest naming your text boxes in search form with some normal prefix like "search" or something like that. For instance, if you have text box "text" and "language" in the form, you will have "searchText" and "searchLanguage". These names are quite unique and you will have normal names in your parameters.
I am not suggesting you populating the hidden values in your results form on POST event since you said it's not an option for you but still it may be the only way if you want to have two forms.
I think the best approach will be storing the text from search input when it changes in the query part of your second form action url. For example (not tested):
$('input#yourSearchInput').change(function()
{
var searchText = $(this).val();
// or? var searchText = encodeURIComponent($(this).val());
var secondForm = $('form#secondFormId');
var action = secondForm.attr('action');
var queryStart = action.lastIndexOf('?search=');
if(queryStart > -1) {
action = action.substring(1, queryStart);
}
action = action + "?search=" + searchText;
secondForm.attr('action', action);
});
In Controller (or custom filter):
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var search = Request.QueryString["search"];
if(!String.IsNullOrEmpty(search)) {
ViewData["SearchFromPOST"] = search;
}
base.OnActionExecuting(filterContext);
}
In your Search Control:
<%= TextBox("yourSearchInputId", ViewData["SearchFromPOST"]) %>

Resources