【问题标题】:MVC Html.DropDownList accessing the text fieldMVC Html.DropDownList 访问文本字段
【发布时间】:2011-01-12 00:19:02
【问题描述】:

我在访问 Html.DropdownList 的选定值中的文本字段时遇到问题。

我的 ViewModel 是

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


    public string DefaultCurrency
    {
        get;
        set;
    }

}

我的控制器填充下拉列表如下。

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

         .........
      }

查看索引

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

不,当我发布/更新时,我会调用不同的操作(比如更新),并且我想访问 Currencycode 以及 CurrencyDescription。我可以获取 Currencycode,但无法访问 CurrencyDescription。

非常感谢任何帮助。

【问题讨论】:

    标签: asp.net-mvc drop-down-menu


    【解决方案1】:

    描述(&lt;option&gt;s 的文本)不是从下拉列表(&lt;select&gt; 元素)的表单中发布的。您可以做的是在服务器上维护描述到值的映射,以便快速查找。或者,您可以简单地使用 SelectListItem 对象的文本和值的描述。

    【讨论】:

    • 谢谢,但不幸的是,我需要 POST 时的“代码”和“描述”。因此不能同时为 SelectListItem 对象设置文本和值。看起来唯一的选择是(不是最好的解决方案)在服务器上维护描述到值的映射以便快速查找。
    【解决方案2】:

    我建议这样做:

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

    【讨论】:

    • 谢谢。我尝试更改 userViewData.SupportedCurrency = GetSupportedCurrencies(); SelectList SupportedCurrencyList = new SelectList(userViewData.SupportedCurrency, "Value", "Text", 但根本没有效果。整行也完全没有效果。我可以完全删除这一行并下拉仍然有效。我不太明白为什么实现这样一个基本功能如此困难。我要做的就是用视图模型(而不是视图数据)填充下拉列表,并在 POST“更新”操作时访问所选值的描述。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 2015-05-31
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多