Go back to a search page without losing search criteria - asp.net

I need some help on this problem. It is about ASP.NET MVC3. I have a page with at the top some search criteria and at the bottom the resulting data. The user can type some criteria and use a submit button to retieve data. In my controller, I have an ActionResult function for managing these criteria and return to the same page with a ViewModel class filled.
The problem: the user can click on a line in the resulting table for viewing a detail page. On the detail page, he can navigate to an edit page for editing data. On this page (edit data) I would like to able the user to go back to the search result page (in fact: navigate back two times). What is the best way to proceed? If I "simply" use an ActionLink (without posting data) to my search result page, it will simply display an empty result page with empty search criteria. Maybe I need to keep my search criteria in a session variable? I don't like this kind of thing...
Any help will be highly appreciated.

Why not place the data in the Session, as you say?
public ActionResult Search(string searchCriteria)
{
Session["searchCriteria"] = searchCriteria;
// do more stuff
}
This way you have the search criteria available no matter how many "back clicks" the user does.
You could make it much more complicated but I do not think it is necessary in this case. If you want to pass it as route data in an action link you'll have to defensively add a searchCriteria parameter to every ActionLink of the pages the user might navigate to from the Search page. That makes it a lot more cumbersome in my opinion.
Good enough is sometimes good enough. Refactor later as needed. :)

Related

Cross-page posting with target _blank leaves source page unfunctional

I'm trying to describe it in as few steps as possible:
I have Page1.aspx with lot of controls, and Preview and Save button among those. I also have Page2.aspx that is the redirection target of a Preview Button click.
Since I need all the controls selections from Page1 to draw a preview on Page2 the redirection is done with setting Preview's PostBackUrl.
I also must have preview shown on a new tab or window so I used onClientClick="aspnetForm.target='_blank'" for Preview button definition.
Save button-click callback, after storing data to a database does redirection to some Page0.aspx (initial list of reports - the subject of the code)
Preview button works fine - a preview renders in a new tab, but when I go to the old tab and click on Save, I see from debugger, that firstly Page2.aspx(?) and secondly Page1.aspx are loaded. Then all the data is stored in the db, but though Page0 redirection is executed Page1.aspx stays loaded in the browser.
I have no idea what processes are behind this. Could one who knows give me an insight? Or if you consider my approach impossible to implement give some idea how to do the same?
If it's of importance, everything on the Page1 is located in an update panel.
Thank you very much for replying
In ASP.NET there are basically zero (0) circumstances in which you will ever send form data from one page to another. Although what exactly you are trying to accomplish is vague, you can consider some of the following:
Isolate unique operations/systems to a single page. If you have something like a User Profile, don't have three different aspx pages; just use a single page for the user or admin to manage that data / functions. Postback events are your friend.
Understand the difference between ViewState and traditional form data. I'm guessing that if you're trying to post form data from one page to another, you probably don't understand the point of ViewState. Using a single page to maintain temporary data that the user is currently working with is a great use for ViewState. If you want the data to appear on another page then you need to consider the data from the previous page as final and thus should be saved to a database or some other medium.
These are just some general guidelines because there is no exact answer to your problem without saying something generic like "You're doing it wrong." I would recommend starting by never again trying to post form data from one aspx page to another.

How to keep a rendered page for later usage (to resend it later)

I have an asp.net application with a search page, with criteria and result display on the same page. I want to keep a copy of the populated search page to redistribute it later to the same user, upon the button click on another page. It's kind of a "return to search" button. How can I do that?
Here is some context:
The search criteria is made up of some basic controls, and the results are then (after postback) displayed in a GridView. I also have a master page. Simple as that.
Now consider the following scenario: The user can investigate the results by clicking links that show detail pages, and can drill down over quite many detail pages with associated data. If he/she wants to get back to the search results he/she needs to click the back button of the browser quite many times.
I would like to provide a "Back to search" button on the master page that allows to return to the populated search page with one click.
Note:
I can not use the browser history in any way because it must work also when the user opened one of the detail views in another tab.
I have seen Keeping the Viewstate persistent and retrieve it on demand but it hope there is an easier solution because my grid is paginated and I have also more than one search page, where I would like to return to just the last one used.
Thanks, Marcel
I can offer some logical ways to resolve this problem, without using specialized asp.net features if they exist:
1) Is there some way to save the search string in GET request? So you can save it some way between moving through pages?
2) Another way is caching search pattern (with all filters or what you need there) somewhere - in database, for example and contain some key in get request, which would point on this pattern.

How to review all datas in one pages in asp.net?

I have five pages in asp.net web application. I entered information in first page and click next. it goes to next page and enter information in this page. I continue this process for five pages. Before click Submit button in fifth page, I need to review once all information entered by me in all previous pages under review button click. How do this? pls Advice.
You can store your data in the database, as you move along the steps, and retrieve it in the final step. You can also store it in Session, as an alternative.
Surely there are other ways to implement this scenario, but the idea is to somehow persist the data and retrieve it in the final step.
I personally think that you are going the wrong way about this. Passing data from page to page is going to cause you all sorts of trouble.
There are a number of methods to achieve this on a single page. You could create 5 seperate controls and place them all on the single page (if they aren't all too big and slow down the page load), then make the appropriate control visible depending on which the user is to see. For example, when you click next make the current control visible property false and then display the next one. Then at the end of the process when you click complete you can access the entered data from each of the controls, you could create a fancy function in the controls that return you a populated class or even pass by reference (we won't go in to that here).
You could also look in to using Views. Which is another method that you could implement on a singular page.
If you're adamant with using multiple pages then you could temporarily store the data in a database and then on the final page get the IDs from a Session and store it all finally as one. But I wouldn't advise this, although it depends on the situation. If you want them to be able to fill half the form and then continue at a later date then the database method would be advisable, but if you only want data being stored upon completion then you will end up with a lot of half completed redundant data using this method.
The solution totally depends on the framework and methods you are using. I'll try and help you with an ASP.Net MVC3 solution, as I think MVC3 is really the way to go for when using ASP.Net.
When the user clicks on the 'next' button, just load a partial view (containing the next step html) with the JavaScript / jQuery Ajax method, OR just present the next step by using using the jQuery show / hide functions.
Keep your form data within one html form-tag (or at least one holder-div).
When you need to submit the information at the last step, simply collect the information with a single jQuery serialize call and send it to your ASP.Net receiver.
Some code snippets:
Display a partial view from the Razor view-engine:
#Html.Partial("/Views/Instance/_General.cshtml", Model)
Load a partial view through jQuery - for more details, please see http://api.jquery.com/category/ajax/
#Html.Partial("/Views/Instance/_General.cshtml", Model)
Post your data to the server though jQuery:
$("#submit-button-id").click(function () {
var data = $("#form-id").serialize();
$.ajax({
url: "/Example/Save",
type: "POST",
data: data,
cache: false,
success: function (success) {
if (success) {
// things worked out just fine..
}
else {
// present some kind of error..
}
}
});
});

What is the right way to handle mode parameters in MVC?

My app has a number of ambient properties, like the current CountryId, DocumentMode, etc. As I learned in a previous question, the current value of these properties should not be stored in the Session, but rather sent in the query string on every page request. So far so good.
So when I build a page, I want to arrange that all the action links look like this:
/controller/action?CountryId=x&DocumentMode=y&...
I can easily do this by checking the query string and slipping in the current value of each of these variables.
The question is, what's the right way to notify the app when one of the values changes?
Specifically, at the top of each view, I have a select dropdown that shows, e.g., all the countries. What should happen when you select a new one?
Right now, the change triggers a javascript function call that replaces the CountryId in the query string, and calls an action that just reloads the original page, but with the new CountryId set, and so the new action links are rebuilt. But this seems sort of kludgy. Is there a more elegant way to just update all the links on the page without needing a server refresh to do this? (I could always cook up some script to do this, but it doesn't seem trivial, and I don't want to reinvent the wheel if there's a built in way to do this.)
Any help much appreciated. Thanks!
You could put the part of the page that changes in a partial view and reload that view via AJAX each time a control is changed.
Partial rendering after page loaded
Alternatively you could just write some javascript to update all the links. Post some code and I'm sure you'll get some suggestions on a good way to write it.
I decided to keep things simple for the time being and just refresh the whole page, which recreates all of the links. My app is low volume and this suffices for now. If I ever need to build an app where the server can't be foolishly stressed like that, I'll see about the swap-in-place solution.

Paging search results with asp.net MVC

I have a situation that I couldn't find a solution for through my searches on here. Here is the scenario:
I have a search form with 2 required fields and multiple optional ones. The form posts to an action method that determines which fields are selected and builds a List<> of objects that match the search criteria. I then pass that List<> to the view for display.
This issue I am running into involves how paging is typically done with asp.net mvc. For past projects I have used a custom Html helper that creates links which include the query parameters as well as a "page" parameter. It then uses a GET request and the .Take().Skip() format.
I've hit a wall on this project as I can't use a GET request for the search criteria and I can't figure out a way to keep the List<> in memory to do the usual "page" parameter trick.
I thought about storing the List<> in the session but the objects and the list could be very large.
I would think this is a popular issue with advanced search forms but I can't seem to find a good solution. Any help would be appreciated. Thanks!
How about cacheing the search result object and giving it a unique key. You would then have your paging links reference that unique (SearchID) and have your action look for that object, pull it from cache and Skip/Take from there.
This will not rebuild the object for every request, making page loading much faster and reducing strain on your database/application.
Here is a article about cacheing:
https://web.archive.org/web/20211020111559/https://aspnet.4guysfromrolla.com/articles/100902-1.aspx
Here is a video about cacheing:
http://www.asp.net/learn/Videos/video-6206.aspx
Note: Be sure you specify expiration date on the cached object.
If I understand properly, you only want to load the search results one time, and then page through them.
Have you looked into any jQuery paging functionality? You can just dump the entire list to the page and use JavaScript to handle the paging (and sorting if you like).
An example can be found at http://beckelman.net/demos/jqueryTableSorterConPaging/default.aspx
Put everything in the same form: the required fields, the optional fields, the page links.
Two possibilities:
Use submit buttons or images instead of anchor tags for the page links each having a different name (e.g. page1, page2, ...): this will allow you to get the desired page when the form is submitted.
Put a hidden field inside your form. Then add a javascript click handler to any of the page anchors. This handler will update the value of the hidden field with the page, submit the form and cancel the event.
So clicking on any of the pager links will submit the form with all the data you need to build the list and pager links.

Resources