【问题标题】:How to populate a ListBox based on a list of a model in asp.net mvc4?如何根据 asp.net mvc4 中的模型列表填充 ListBox?
【发布时间】:2013-08-09 20:21:27
【问题描述】:

我有一个强类型视图,其中有一个基于模型联系人的表单。在文本框中,默认值是我传递给控制器​​视图的联系人的值。 所以我有一个类 Contact 如下:

public class Contact
    {
        public int IdContact { get; set; }
        public string Nom { get; set; }
        public string Prenom { get; set; }
        public string Email { get; set; }
        public string Fonction { get; set; }
        public List<FonctionContact> ListeFonctions = new List<FonctionContact>();
        public string TelephoneFixe { get; set; }
        public string TelephonePort { get; set; }
        public Contact() { }

        public Contact(int idContact, string nom, string prenom, string email, string telephoneFixe, string telephonePort)
        {
            this.IdContact = idContact;
            this.Nom = nom;
            this.Prenom = prenom;
            this.Email = email;
            this.TelephoneFixe = telephoneFixe;
            this.TelephonePort = telephonePort;
        }

        public Contact(int idContact, string nom, string prenom, List<FonctionContact> listeFonctions, string email, string telephoneFixe, string telephonePort)
        {
            this.IdContact = idContact;
            this.Nom = nom;
            this.Prenom = prenom;
            this.ListeFonctions = listeFonctions;
            this.Email = email;
            this.TelephoneFixe = telephoneFixe;
            this.TelephonePort = telephonePort;
        }
    }

有一个FonctionContact列表。 FonctionContact 的类:

public class FonctionContact
{
    public int IdFonction;
    public string LibelleFonction;

    public FonctionContact() { }

    public FonctionContact(int idFonction, string libelleFonction)
    {
        this.IdFonction = idFonction;
        this.LibelleFonction = libelleFonction;
    }
}

所以我想在 listBoxfor 中显示联系人的 ListeFonctions 属性,但它不起作用。我尝试在表单中显示列表:

@using (Html.BeginForm()){ 
     <label>Nom:</label>
     @Html.TextBoxFor(contact => contact.Nom, new { @Value = @Model.Nom })
     <label>Prénom:</label>
     @Html.TextBoxFor(contact => contact.Prenom, new { @Value = @Model.Prenom })
     ///the next controls for the next properties of class Contact...
     <label>Fonction(s):</label>
     @Html.ListBoxFor(contact => contact.ListeFonctions, new SelectList(Model.ListeFonctions, "IdFonction", "LibelleFonction"));
     }

它向我显示一个错误:“Model.FonctionContact 没有属性 IdFonction。所以我被困在这里,我无法找出问题所在。有人有想法吗?

【问题讨论】:

    标签: c#-4.0 asp.net-mvc-4 html.listboxfor


    【解决方案1】:

    基本上,您需要为 ListBoxFor 提供一个值项列表(整数,您可以使用它来加载一些以前选择和保存的项)。此外,它需要一个 MultiSelectList 作为第二个参数(出于前面解释的原因,因为它不是一个只有一个选定项目的 DropDownList),这可能更适合在模型中组合,如下所示:

    型号

    public class Contact
            {
                public int IdContact { get; set; }
                public string Nom { get; set; }
                public string Prenom { get; set; }
                public string Email { get; set; }
                public string Fonction { get; set; }
    
                // you could save a selection of items from that list
                private List<int> _selectedFonctionIds;        
                public List<int> SelectedFonctionIds{
                    get {
                        return _selectedFonctionIds?? new List<int>();
                    }
                    set {
                        _selectedFonctionIds= value;
                    }
                }
                private List<FonctionContact> _listeFonctions;
                public MultiSelectList ListeFonctions{
                   get {
                      return new MultiSelectList(
                                _listeFonctions,
                                "IdFonction", // dataValueField
                                "LibelleFonction" // dataTextField
                      );
                       }
                }
                public string TelephoneFixe { get; set; }
                public string TelephonePort { get; set; }
                public Contact() { }
    
                public Contact(int idContact, string nom, string prenom, string email, string telephoneFixe, string telephonePort)
                {
                    this.IdContact = idContact;
                    this.Nom = nom;
                    this.Prenom = prenom;
                    this.Email = email;
                    this.TelephoneFixe = telephoneFixe;
                    this.TelephonePort = telephonePort;
                }
    
                public Contact(int idContact, string nom, string prenom, List<int> selectedFonctionIds, List<FonctionContact> listeFonctions, string email, string telephoneFixe, string telephonePort)
                {
                    this.IdContact = idContact;
                    this.Nom = nom;
                    this.Prenom = prenom;
                    this.SelectedFonctionIds = selectedFonctionIds;
                    this._listeFonctions = listeFonctions;
                    this.Email = email;
                    this.TelephoneFixe = telephoneFixe;
                    this.TelephonePort = telephonePort;
                }
            }
    

    在视图的表单中

    @Html.ListBoxFor(contact => contact.SelectedFonctionIds, Model.ListeFonctions)
    

    【讨论】:

    • 我试过了,但它显示了一个数据绑定错误,说 Class FonctionContact 没有像 IdFonction 这样的属性
    • 您是否尝试过将 IdFonction 设为属性(public int IdFonction { get; set; } 而不仅仅是 public int IdFonction;)? LibelleFonction 也应该这样做。
    • ... 累了就不要工作了!它有效!,非常感谢@Mark
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2016-11-25
    • 1970-01-01
    相关资源
    最近更新 更多