如何根据asp.net mvc4中的模型列表填充ListBox?

时间:2013-08-08 07:48:09

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

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

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。所以我被困在这里,我找不到有什么问题。有人有想法吗?

1 个答案:

答案 0 :(得分: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)