簡體   English   中英

如何在asp.net mvc4中基於模型列表填充列表框?

[英]How to populate a ListBox based on a list of a model in asp.net mvc4?

我有一個強類型視圖,其中有一個基於模型聯系人的表單。 在文本框中,默認值是我要傳遞給控制器​​中視圖的聯系人的默認值。 所以我有一個類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列表。 功能類別:

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

    public FonctionContact() { }

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

因此,我想在listBoxfor中顯示Contact的屬性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。所以我被困在這里,我找不到問題所在。有人有主意嗎?

基本上,您需要為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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM