views:

322

answers:

2

Hi 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)
    {
        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.

A: 

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.

Jacob
Thanks but unfortunately I need both "Code" and the "Description" upon the POST. So cannot set both text and the value for the SelectListItem objects. Looks like the only option is (not the best solution) maintain a mapping of descriptions to values on your server for quick lookups.
Rajithakba
A: 

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);

     .........
  }
Francisco
Thanks. I tried changing the userViewData.SupportedCurrency = GetSupportedCurrencies(); SelectList SupportedCurrencyList = new SelectList(userViewData.SupportedCurrency, "Value", "Text",But no effect at all. Also the whole line has no effect at all. I can remove this line completely and drop down still works. I cannot really understand why it is so hard to implement such a basic functionality. All I want to do is having dropdown populated with a View Model (not Viewdata) and access the description of the selected value upon a POST "Update" action.
Rajithakba