简体   繁体   English

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

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

I have a strongly typed view where I have a form based on a Model Contact. 我有一个强类型视图,其中有一个基于模型联系人的表单。 In the textboxes, the default values are those of the contact that I'm passing to the view in the controller. 在文本框中,默认值是我要传递给控制器​​中视图的联系人的默认值。 So I have a class Contact as follows: 所以我有一个类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;
        }
    }

There is a list of FonctionContact . 有一个FonctionContact列表。 The class of FonctionContact: 功能类别:

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

    public FonctionContact() { }

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

So I would like to display the property ListeFonctions of the Contact in a listBoxfor but it doesn't work. 因此,我想在listBoxfor中显示Contact的属性ListeFonctions ,但它不起作用。 There is my form where I've tried to display the list : 我尝试在表单中显示列表:

@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"));
     }

It shows me an error: "Model.FonctionContact doesn't have a property IdFonction. So I'm stuck here, I can't find out what's wrong. Somebody has an idea ? 它向我显示了一个错误:“ Model.FonctionContact没有属性IdFonction。所以我被困在这里,我找不到问题所在。有人有主意吗?

Basically, you'll need to provide the ListBoxFor with a list of value items (integers, which you could use to load some previously selected and saved items). 基本上,您需要为ListBoxFor提供值项列表(整数,可用于加载一些先前选择和保存的项)。 Also, it will need a MultiSelectList as the second parameter (for the previously explained reason, because it's not a DropDownList with just one selected item), which would probably be neater to compose in the model, as I have written below: 另外,它将需要一个MultiSelectList作为第二个参数(由于前面解释的原因,因为它不是一个仅包含一个选定项目的DropDownList),所以在模型中编写它可能会更整洁,正如我在下面编写的那样:

Model 模型

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;
            }
        }

inside the view's form 在视图的窗体内

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM