Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AnithaUnitedhands 4 posts 44 karma points
    Jul 03, 2023 @ 04:53
    AnithaUnitedhands
    0

    Implementation of Currency Conversion in Umbraco 10

    Hi, I am using Umbraco 10. I want to implement a currency conversion on my website. For that, I have two methods in the controller and I have used begin form in the view file. Now, I am unable to get the value in the textbox but while debugging I got the correct value. Please help me to display the value in the text box. Thanks in Advance.

    My Controller:

    using Microsoft.AspNetCore.Mvc;

    using Microsoft.Extensions.Logging;

    using Microsoft.Extensions.Options;

    using MimeKit;

    using Newtonsoft.Json;

    using Newtonsoft.Json.Linq;

    using System.Net;

    using System.Threading.Tasks;

    using Umbraco.Cms.Core.Cache;

    using Umbraco.Cms.Core.Configuration.Models;

    using Umbraco.Cms.Core.Logging;

    using Umbraco.Cms.Core.Mail;

    using Umbraco.Cms.Core.Models.Email;

    using Umbraco.Cms.Core.Routing;

    using Umbraco.Cms.Core.Services;

    using Umbraco.Cms.Core.Web;

    using Umbraco.Cms.Infrastructure.Persistence;

    using Umbraco.Cms.Web.Common.PublishedModels;

    using Umbraco.Cms.Web.Website.Controllers;

    namespace Umbraco.Cms.Web.Common.Controllers

    {

    public class CurrencyConversionController : SurfaceController
    
    {
    
        private readonly ILogger<CurrencyConversionController> _logger;
        private readonly UmbracoHelper _umbracoHelper;
    
        public CurrencyConversionController(
            IUmbracoContextAccessor umbracoContextAccessor,
            IUmbracoDatabaseFactory databaseFactory,
            ServiceContext services,
            AppCaches appCaches,
            IProfilingLogger profilingLogger,
            IPublishedUrlProvider publishedUrlProvider,
            UmbracoHelper umbracoHelper, 
            ILogger<CurrencyConversionController> logger) 
            : base(umbracoContextAccessor, databaseFactory,
                  services, appCaches, profilingLogger,
                  publishedUrlProvider)
        {
            _logger = logger;
        }
    
    
        [HttpPost]
        public async Task<IActionResult> Convert(double Amount)
        {
            if (!ModelState.IsValid)
            {
                return CurrentUmbracoPage();
            }
           //TempData["Success"] = await CurrencyExchanger(Amount);
           var r = await CurrencyExchanger(Amount);
            ViewBag.CurrencyValue = r;
            return Content(r);
        }
    
        public async Task<string> CurrencyExchanger(double Amount)
        {
            const string FROM_CURRENCY = "ZAR";
            const string TO_CURRENCY = "INR";
    
            string apiKey = "28b685e03d2b4c14a0747bdbc32b8d4d";
            // Console.WriteLine(apiKey);
            string apiUrl = $"https://openexchangerates.org/api/latest.json?app_id={apiKey}";
            string responseJson;
    
            using (var client = new WebClient())
            {
                responseJson = client.DownloadString(apiUrl);
            }
            var response = JObject.Parse(responseJson);
    
            //Currency Conversion Calculation
    
                if(FROM_CURRENCY  != TO_CURRENCY)
                {
                    double exchangeRate = (double)response["rates"][FROM_CURRENCY] / (double)response["rates"][TO_CURRENCY];
                    double result = Amount * exchangeRate;
                    return result.ToString("N0");
                }
    
        }
    }
    

    }

    View file:

    @using (@Html.BeginForm("Convert", "CurrencyConversion", FormMethod.Post))

    {

                        <div class="Rand_style">@Model?.SouthAfricanCurrency</div>
                        <div class="INR_style">@Model?.IndianCurrency</div>
                        <span> @date <span>  UTC . Disclaimer</span> </span>
    
                        <div class="input_wrp_box">
                            <div class="input_style"><input type="text" placeholder="1" name="Amount"> </div>
    
                            <div class="right_map">South Africa Rand <img src="~/assets/images/south_africa.png" /></div>
                        </div>
    
                        <div class="input_wrp_box">
                            <div class="input_style"><input type="text" placeholder="4.91" value="@ViewBag.CurrencyValue" /></div>
                            <div class="right_map">India <img src="~/assets/images/india.png" /></div>
                        </div>
    
                            <div class="clearfix"></div>
                        <div id="resultDiv" style="display: none;"></div>
                        <button type="submit" class="btn_style" >Convert</button>
                    </div>
                }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies