首页 > 解决方案 > 如何从 C# 中的 Drodownlist 中获取值

问题描述

GetCurrency(countrydetials.Country)给了我currency那个特定的国家。但在我的情况下,我的 Currency下拉列表中只有两种货币。如果country匹配项currency在下拉列表中不可用,currency则默认情况下应选择Indian Rupee

控制器代码

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

看法

@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"})

标签: c#asp.net-mvc

解决方案


推荐阅读