Storing values ​​in ASP.NET MVC

Background

I have a page in my ASP.NET MVC web application for users to enter addresses into their address book. When the user enters their address and clicks the submit button, the controller action that processes the form post then geocodes the address (retrieves the latitude, longitude, and full address if the user forgot to enter their state, zip code, etc.).

It is now entirely possible that the user did not enter enough information to get an exact match. In this case, the geocode service returns a list of the best matches for the specified address. When this happens, I want to invite a user with this list of best matches so they can choose which address they want to use.

This all works great. I keep a list of the best matches in ViewData, and the page displays the items in the list as an unordered list if provided. Next to each of the addresses is a Select link that the user can use to select the address they want to use. The "click" event of the "Select" link is hooked up using jQuery to replace the text in the source url text box with the address the user chooses, and then click the submit button via javascript.

Problem

When the user selects an address, I want my controller action to check if the address is in their address book. If so, I want to return an error message indicating that this address already exists. I also want to re-display the list of possible addresses for the user to select (for example, if the user initially saw a list of 3 addresses to choose from and they chose the one that was already in their address book, the page should show an error message and 3 addresses to reselect). How do I redraw the best match list without making another call to the geocoding service in my controller action?

In other words, I have an unordered list that was generated by the last controller action that I want to display to the user again. The items in the list were displayed as text strings and therefore were not dispatched to my controller action. The only way I have been able to solve this so far is to keep a match list of addresses in the session. Am I missing something or is this how I should do it?

In an ASP.NET Web Forms application, I could simply store the best matches in a ViewState, and it will be available to me on postback from the user who chooses the address. Is there a similar mechanism for ASP.NET MVC?

0


source to share


3 answers


You can use an AJAX request to the controller that will check if the url exists, and only it updates only the error message container on the page. This way, the already submitted hit list will remain intact.



+2


source


Why don't you filter the existing address in the user's address book before showing them all the choices? Thus, the user will not be able to select an already existing address, because he will never see it as an option.



+1


source


save addresses in a table with an identifier. Save the identifier in the user list. It is now easy to filter the list by checking if the ID exists in the user list.

0


source







All Articles