【问题标题】:Languages drop down menu in MVC [closed]MVC中的语言下拉菜单[关闭]
【发布时间】:2017-09-20 03:42:38
【问题描述】:

我想为语言选择创建一个下拉菜单。它是一个 MVC 数据库应用程序。我希望用户能够选择多种语言。

我想知道如何将我的模型连接到剃刀视图。在类的属性之上我可以使用什么样的属性?

这是我拥有的模型类的快照:

public class CrewViewModel
    {
 // This is the date of birth property
        [Required]
        [DisplayName("Date of Birth")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-dd-mm}", ApplyFormatInEditMode = true)]
        public DateTime DOB { get;  
 // Here goes the code for the Languages property
    }

以及在视图中使用什么剃须刀代码。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 razor


    【解决方案1】:

    有很多方法可以做到这一点,我过去通过创建一个特定的类来连接我的价值观来做到这一点:

    public class LanguageItem
    {
        public bool Disabled { get; set; }
        public string Value { get; set; }
        public string Name { get; set; }
    }
    

    然后你可以在你的模型中创建你的属性:

    public IEnumerable<LanguageItem> Languages
    {
        get
        {
             return new List<LanguageItem>
              {
                  new LanguageItem {Name = "English", Value = "en-US"}
              };
        }
    }
    

    或者无论如何你填充你的。为选择创建一个字段(即 SelectedLanguage)。然后像......

     @Html.DropDownListFor(m => m.SelectedLanguage, new SelectList(Model.Languages, "Value", "Text"), "Language")
    

    应该有效

    【讨论】:

    • 我不断收到 System.NullReferenceException
    猜你喜欢
    • 2018-11-10
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2021-11-17
    • 2022-11-19
    • 1970-01-01
    相关资源
    最近更新 更多