0

GetCurrency(countrydetials.Country) gives me currency of that specific country. But in my scenario, I only have two currencies in my Currency dropdownlist. If the country matches currency is not available in currency dropdownlist then by default it should select Indian Rupee.

Controller code

 CountryDetials countrydetials = new CountryDetials();
 var model = GetCurrency(countrydetials.Country);
 countrydetials.Currency = model.FirstOrDefault().Currency;   

View

@Html.DropDownListFor(m => m.Currency, new List<SelectListItem>
                  {
                    new SelectListItem {Value = "INR", Text="Indian Rupee" },               
                    new SelectListItem {Value = "NPR", Text = "Nepalese Rupee" },               

                  },
                    new { @class = "form-control" })



  @Html.DropDownListFor(m => m.Country, new List<SelectListItem>
                           {

                               new SelectListItem {Value = "IND", Text = "India" },
                               new SelectListItem {Value = "NEP", Text = "Nepal" },
                               new SelectListItem {Value = "SIN", Text = "Singapore" },
                               new SelectListItem {Value = "USA", Text = "USA" },

                           },
                                new { @class = "form-control"})
MVC
  • 649
  • 7
  • 23
  • 1
    SO what's your question? – Umang Jul 17 '18 at 00:15
  • @Umang, as you can see, there are 2 currency and 4 country listed in my 2 dropdownlists. If I select country India then It will give me Currency code `INR` which is available in Currency dropdownlist. But If I select Country `USA` then `GetCurrency(countrydetials.Country)` gives me currency code `USD` from database table but `USD` is not listed in Currency dropdownlist. So at this point of time, I want `INR` should be selected. – MVC Jul 17 '18 at 00:21
  • Why is the data returned by `GetCurrency` and the items in currency dropdown not matching ? Looks like you have some data issue. To set an option selected, set the property value to that (`countryDetails.Currency="INR";`) – Shyju Jul 17 '18 at 00:48
  • @Shyju, Actually I want to compare value returned from `GetCurrency` and `countryDetails.Currency`. which is there is dropdownlist. If both value matches then display Currency text in dropdown else set `Indian Rupee` . – MVC Jul 17 '18 at 01:21
  • Have you thought about writing an if condition to check that ? – Shyju Jul 17 '18 at 01:24
  • @Shyju, Yes I was thinking to write if condition. But my problem is that how to get value from `countryDetails.Currency`. – MVC Jul 17 '18 at 01:28
  • It feels like you should something like JQuery on client side to have intended behaviour i.e., when country selected is USD, then Currency is INR if the options dont have USD. But I also feel you are missing a link which Translate from Country selected to USD. I also suggest to use data attribute to do translation similar to here: https://stackoverflow.com/questions/8345666/on-select-change-get-data-attribute-value – Umang Jul 17 '18 at 02:29

0 Answers0