【问题标题】:Is there a way to dynamically populate a drop down list with values from a resource file?有没有办法用资源文件中的值动态填充下拉列表?
【发布时间】:2009-07-21 09:12:26
【问题描述】:

所以我有这个 .resx 文件,我希望它的值显示在 ASP.NET MVC (C#) 的下拉列表中。 这可能吗? Google 帮不了我,所以我希望 SO 可以:-)

【问题讨论】:

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


    【解决方案1】:

    这对我有用

    Html.DropDownList("ResxDropDownList",
        new SelectList(
            Resources.YourResource.ResourceManager.GetResourceSet(
                System.Globalization.CultureInfo.CurrentCulture,
                true,
                true
            ),
            "Key",
            "Value"
        )
    )
    

    【讨论】:

      【解决方案2】:

      这实际上取决于您如何将值保存在 RESX 中。 假设您将值保存为字符串。

      App_GlobalResources/Messages.resx:

      姓名 |价值 --------------------- 标题 |先生夫人女士。
      List<SelectListItem> items = new List<SelectListItem>();
      foreach (string s in Resources.Messages.title.Split(new char[] { ',' }))
      {
         items.Add(new SelectListItem() { Text = s, Value = s });
      }
      Response.Write(Html.DropDownList("Title", items));
      

      【讨论】:

        猜你喜欢
        • 2020-06-26
        • 1970-01-01
        • 2021-09-12
        • 2022-12-31
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2015-12-11
        相关资源
        最近更新 更多