开发者

MVC Html.DropDownList accessing the text field

I'm having problem accessing the text field in a selected value of Html.DropdownList.

My ViewModel is

public class UserViewModel
{     
    public List<SelectListItem> SupportedCurrency
    {
        get;
        set;
    }


    public string DefaultCurrency
    {
        get;
        set;
    }

}

My controller populates the dropdown list as bellow.

public List<SelectListItem> GetSupportedCurrencies(string setupValue)
    开发者_StackOverflow社区{
        List<SelectListItem> items = new List<SelectListItem>();

        try
        {
            IList<Currency> currencyList  = Helper.GetFormattedCurrenciesList(CurrenciesService.GetSupportedCurrencies());

            foreach (Currency c in currencyList)
            {
                if (!string.IsNullOrEmpty(setupValue) && c.CurrencyCode.Equals(setupValue))
                {                        
                    items.Add(new SelectListItem
                    {
                        Text = c.CurrencyDescription,
                        Value = c.CurrencyCode,
                        Selected = true
                    });
                }
                else
                {
                    items.Add(new SelectListItem
                    {
                        Text = c.CurrencyDescription,
                        Value = c.CurrencyCode
                    });
                }
            }
        }
        catch (Exception ex)
        {
           throw ex
        }

        return items;
    }



[AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Index()
    {

     userViewData.SupportedCurrency = GetSupportedCurrencies(userModelData.DefaultCurrency);
            SelectList SupportedCurrencyList = new SelectList(userViewData.SupportedCurrency, "CurrencyCode", "CurrencyDescription");

         .........
      }

In View Index <%= Html.DropDownList("userViewModel.DefaultCurrency", Model.SupportedCurrency)%>

......................

No when I do post/update I call a different action (say Update) and I want to access the Currencycode as well as CurrencyDescription. I can get Currencycode but I can't access CurrencyDescription.

Any help greatly appreciated.


The description (text of the <option>s) isn't posted from a form for a dropdown list (<select> element). What you could do is maintain a mapping of descriptions to values on your server for quick lookups. Or you could simply use the description for both the text and the value for the SelectListItem objects.


I would suggest doing this:

public List<SelectListItem> GetSupportedCurrencies()
{
    List<SelectListItem> items = new List<SelectListItem>();

    try
    {
        IList<Currency> currencyList  = Helper.GetFormattedCurrenciesList(CurrenciesService.GetSupportedCurrencies());

        foreach (Currency c in currencyList)
        {
            items.Add(new SelectListItem
            {
                Text = c.CurrencyDescription,
                Value = c.CurrencyCode
            });
        }
    }
    catch (Exception ex)
    {
       throw ex
    }

    return items;
}

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{

 userViewData.SupportedCurrency = GetSupportedCurrencies();
        SelectList SupportedCurrencyList = new SelectList(userViewData.SupportedCurrency, "Value", "Text", userModelData.DefaultCurrency);

     .........
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜