tags:

views:

236

answers:

3

Background

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

Now, it's entirely possible that the user didn't enter in enough information to get an exact match. In this event, the geocode service returns a list of best matches for the address specified. When this happens, I want to prompt the user with this list of best matches so that they can select which address they wish to use.

This is all working wonderfully. I store the list of best matches in the ViewData, and the page renders the items in the list as an unordered list if they are provided. Next to each of the addresses is a "Select" link which the user can use to select the address they wish to use. The "click" event of the "Select" link is hooked up using jQuery to replace the text in the original address textbox with the address the user is selecting and then click the submit button via javascript.

The Problem

When the user selects an address, I want my controller action to check if the address is already in their address book. If it is, then I want to return an error message indicating that this address already exists. I also want to re-render the list of possible addresses for the user to select from (e.g. if the user originally saw a list of 3 addresses to pick from and they choose one that was already in their address book, the page should show an error message and the 3 addresses to pick from again). How can I re-render the list of best matches 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 rendered as text strings and therefore aren't posted to my controller action. The only way I have been able to solve this so far is to store the list of address matches in session. Am I missing something or is that the way I should be going about this?

With an ASP.NET web forms application, I could simply store the best matches in ViewState and it would be available to me on the postback from the user selecting the address. Is there a similar mechanism for ASP.NET MVC?

+1  A: 

Why don't you filter the existing address on the user address book before showing him all the options to choose? This way the user won't be able to choose an already existing address because it will never see it as an option.

Augusto Radtke
How would I determine if there is a match? If the user enters in "123 fake st" and internally it's stored as "123 fake st city, state, zip, country", I won't know whether there's a match or not.
Kevin Pang
I suppose I could geocode the address, then remove any best matches that don't already exist in the user's address book...
Kevin Pang
store the addresses in a table with an ID. Store the ID of the in the user's list. Now it is easy to filter the list by checking if the ID exists in the user's list.
Matthew
+2  A: 

You could use an AJAX request to your controller that will verify if the address already exists and if it does only update the error message container of the page. This way the already rendered list of matches will stay intact.

Darin Dimitrov
Ah yes, good idea! Thanks!
Kevin Pang
A: 

store the addresses in a table with an ID. Store the ID of the in the user's list. Now it is easy to filter the list by checking if the ID exists in the user's list.

Matthew