If you find this article useful, consider making a small donation to show your support for this web site and its content.
Free app Developer Interview available here.

Available on the iPhone App Store
Available on the Google Play
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.



Building currency convertor using ASP.NET MVC with Ajax

by Farooq Kaiser 20. August 2009 15:58

ASP.NET AJAX enables a Web application to retrieve data from the server asynchronously and to refresh parts of the existing page. This improves the user experience by making the Web application more responsive and you to create amazing Web 2.0 applications to delight your users. In this article, I will show you how to build an AJAX-enabled ASP.NET MVC application. To create a new MVC project, see ASP.net MVC application structure. I will consume a currency convertor web service which is freely available at WebserviceX.NET.

To reference the ASP.NET AJAX script libraries, add the following markup at the end of the head element in Site.Master:

<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.js") %>" 
        type="text/javascript"></script>  
    <script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.js") %>" 
        type="text/javascript"></script> 

To reference the web service, Right click on project and then click on add web reference as shown below.

 

Add the following code after the About method in HomeController.cs

  public string getConversionRate(string CurrencyFrom, string CurrencyTo)
        {
            CurrencyConvertor curConvertor = new CurrencyConvertor();
            double rate = curConvertor.ConversionRate((Currency)Enum.Parse(typeof(Currency), CurrencyFrom), (Currency)Enum.Parse(typeof(Currency), CurrencyTo)); 
            return rate.ToString(); 
        }

     Add the following code in index view.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Currency Converter
    </h2>
    <% using (Ajax.BeginForm("getConversionRate", new AjaxOptions { UpdateTargetId = "Result" }))
       { %>
    <%= Html.DropDownList( 
                    "CurrencyFrom", 
                     new [] 
                         { 
                                 new SelectListItem 
                                     { 
                                             Text = "Canada", 
                                             Value = "CAD" 
                                     }, 
                                  new SelectListItem 
                                     { 
                                             Text = "USA", 
                                             Value = "USD" 
                                     }, 
                                  new SelectListItem 
                                     { 
                                             Text = "UK", 
                                             Value = "GBP" 
                                     } 
                }, 
               "From this currency:"   
             ) %>
    <%= Html.DropDownList( 
                    "CurrencyTo", 
                     new [] 
                         { 
                                 new SelectListItem 
                                     { 
                                             Text = "Canada", 
                                             Value = "CAD" 
                                     }, 
                                  new SelectListItem 
                                     { 
                                             Text = "USA", 
                                             Value = "USD" 
                                     }, 
                                  new SelectListItem 
                                     { 
                                             Text = "UK", 
                                             Value = "GBP" 
                                     } 
                }, 
               "To this currency:"   
             ) %>
    <input type="submit" value="Submit" /><br />
    <h1>
        <span id="Result"></span>
    </h1>
    <% } %>
</asp:Content>

Now you can run the application and it will render the page as shown below.

Summary 

In this article, we examined AJAX with asp.net MVC. We built a currency convertor application by consuming web service which is freely available at WebserviceX.NET. To download the source code please go to http://www.codeproject.com/KB/ajax/AJAX_with_ASP_NET_MVC.aspx

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

.NET | asp.net | C#


comments powered by Disqus

Comments

Jobs Autos Real estate Videos Power by Google