Introduction
This article explains how to create a Search Finder using ASP.NET MVC that can help to find a Pub near your location.
Step 1
First of all you need to create a database where Pubs are to be stored.
You can use the database that are attached with my application code and then can be attached to your application.
Now we will add a Controller Class in the Controller in which an Action will return the View.
It's coding will be like this:
- public ActionResult StoreLocator(string address)
- {
-
- if (string.IsNullOrEmpty(address))
- return View();
- var output = GoogleMapsAPIHelpersCS.GetGeocodingSearchResults(address);
- var rsltCnt = output.Elements("result").Count();
- if (rsltCnt == 0)
- {
-
- ViewData["NoResults"] = true;
- return View();
- }
- else if (rsltCnt == 1)
- {
-
- return RedirectToAction("StoreLocatorResults", new { Address = output.Element("result").Element("formatted_address").Value });
- }
- else
- {
-
- var same = from result in output.Elements("result")
- let formatted_address = result.Element("formatted_address").Value
- select formatted_address;
- ViewData["Matches"] = same;
- return View();
- }
- }
In this code, first the application will check whether the user provided an address or not, if not then it will return the user to the view where he will see the same page and will need to provide the address or name of city.
If he has provided an address then it will use the Google's Geolocator Service and will return one of the Pubs at that location, if it finds that the address provided by the user is not understandable then it will ask the user to provide an understandable address or city name.
Step 2
Now we will add an Index File to the application where a form will be created, on this form the user will find a TextBox and a Button, in this TextBox the user will be asked to provide the location.
- <% using (Html.BeginForm())
- { %>
- <p>
- <b>Enter Something:</b>
- <%: Html.TextBox("address")%>
- <input type="submit" value="Search!" />
- <br />
- <i>Example: Delhi, India</i> or <i>011</i> or <i>Shastri Nagar, Ghaziabad</i>
- </p>
- <% if (ViewData["NoResults"] != null) { %>
- <div class="input-validation-error">
- The Address you are providing is not understood please type your city name or zip code.
- </div>
- <% } %>
- <%
- var possibleMatches = ViewData["Matches"] as IEnumerable<string>;
- if (possibleMatches != null) { %>
- <div style="padding-left: 25px; margin-top: 10px;">
- <b>Did you mean...</b>
- <ol>
- <% foreach (var find in possibleMatches) { %>
- <li><%: Html.ActionLink(find, "PubFinderResults", new { Address = find }) %></li>
- <% } %>
- </ol>
- </div>
Step 3
We will also create a default index file that will ask the user whether he is looking for a pub! If yes then he will be taken to the index file that we had created previously.
- <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
- <h2>Looking for a Pub! Try the Pub Finder</h2>
- <p>
- <%: Html.ActionLink("Looking for a Pub!", "StoreLocator") %>
- </p>
- </asp:Content>
Output
It's output will be like the following.
First of all you need to click on one of the links that will take you to the Search Finder.
Now on the Next Page provide the name of the city or the address where you want to find a Pub and then click on the "Search" button. If there are many places similar to the place that you are looking for then it will provide all the similar locations and then you can choose one of them.
You can download the code that is provided above.