ASP.NET MVC Form Hosting Scenario Question

I currently have a custom control that is used in both / Address / Edit and / Address / Create. This custom control simply has the following code in it to submit a new address to the system:

<% 
    using (Html.BeginForm())
    {
%>
        <table>
            <tr>
                <td>Street Address</td>
                <td>
                    <%= Html.TextBox("StreetAddress", (ViewData.Model != null) ? ViewData.Model.StreetAddress : "") %>
                    <%= Html.ValidationMessage("Address.StreetAddress") %>
                </td>
            </tr>
        </table>
        <%= Html.SubmitButton() %>
        <%
            if (ViewData["GeocodeResults"] != null) {
        %>      
            <p>
                Google maps found the following addresses that matched the address you entered.  Please select
                the appropriate address.  If none of these addresses are correct, try reentering the address
                again and be as specific as possible.
            </p>
            <ul>
                <% 
                    foreach (GeocodeResult geocodeResult in (List<GeocodeResult>)ViewData["GeocodeResults"]) { 
                %>
                        <li>
                            <%= geocodeResult.StreetAddress %>
                        </li>
                <% 
                   } 
                %>
            </ul>
        <%  
            } 
        %>
<%
    }
%>

      

To summarize the code above, what it does is in the controller, it asks Google Maps to geocode the address in the textbox (i.e. turns it into a set of longitude / latitude coordinates). When Google Maps returns more than one result, I store those results in ViewData ["GeocodeResults"], which then displays possible addresses for the end user.

Now this works fine for displaying addresses, but I really want the list to appear as a list of hyperlinks so that the user can click on the appropriate address and the form will submit with that address instead of the one in the textbox. Is there a way to do this?

+1


source to share


1 answer


Something like:

<a href='javascript:void(0);' onclick='submitAddress(this);'>
    <%= geocodeResult.StreetAddress %></a>

      

where are you



function submitAddress(link) {
   $('input#streetAddress:first').text(link.innerHtml);
   $('input#submit').click();
}

      

You can also remove it in a hidden field, which would mean that you don't have to do a Google map search for that address. That is, if HiddenStreetAddress is provided, just use without searching. If not, do a Google search on StreetAddress. If there are multiple results, display the results. Otherwise, use the ones provided.

+2


source







All Articles