View not binding correcty with the model

2019-07-23 11:25发布

问题:

I can figure out why it's not binding. So I have a form where a ListBox is in a partial view which I reload everytime I click on a checkbox to fill the listbox.

The code of my ModelView for the form is :

                            <div class="row-fluid">
                                <div class="span3">
                                     <label>Fonction(s):</label>
                                </div>
                                <div class="span9" id="ListeFonction">                                        
                                    @Html.Partial("ListerFonction", Model)                                 
                                </div>
                            </div>


                            <div class="row-fluid">
                                <div class="span5 offset3">
                                    <div class="fonctions_container">
                                            @foreach (extranetClient.Models.Classes.FonctionContact fonction in ViewBag.Fonctions)
                                            {
                                                string coche = "";
                                                if ((@Model.ListeFonctions).Any(c => c.IdFonction == fonction.IdFonction))
                                                {
                                                    coche = "checked";
                                                }

                                                <input type="checkbox" @coche class="checkbox" value="@fonction.IdFonction" />@fonction.LibelleFonction <br />
                                            }
                                    </div> 
                                </div>
                            </div>

So as you can see, I render a partial view just after the "Email" Textbox. The code for it is :

@Html.LabelFor(contact => contact.SelectedFonctionIds, "ListeFonctions")
@Html.ListBoxFor(contact => contact.SelectedFonctionIds, new MultiSelectList(Model.ListeFonctions, "IdFonction", "LibelleFonction"), new { disabled = "disabled")

The model associated to that view looks like that:

    private List<int> _selectedFonctionIds;
    public List<int> SelectedFonctionIds
    {
        get
        {
            return _selectedFonctionIds ?? new List<int>();
        }
        set
        {
            _selectedFonctionIds = value;
        }
    }

    public List<FonctionContact> ListeFonctions = new List<FonctionContact>();
    public MultiSelectList ListeFonctionsSelectList
    {
        get
        {
            return new MultiSelectList(
                      ListeFonctions,
                      "IdFonction", // dataValueField
                      "LibelleFonction" // dataTextField
            );
        }
    }

    public Contact() { }

    public Contact( List<FonctionContact> listeFonctions, List<int> selectedFonctionIds)
    {
        this.ListeFonctions = listeFonctions;
        this.SelectedFonctionIds = selectedFonctionIds;
    }

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

But the ListBox of the partial view is not binding with the model. I get well the other informations but not these in the listbox. Somebody has an idea ?

回答1:

Why are you forcing the ListBox's id here:

@Html.ListBoxFor(contact => contact.SelectedFonctionIds, 
   new MultiSelectList(Model.ListeFonctions, "IdFonction", "LibelleFonction"), 
   new { disabled = "disabled", **id="idFonctions"** })

ListBoxFor helper is supposed to generate the ListBox's id for you, and the Id should be the same as the attribute it should bind with. Shouldn't it be SelectedFonctionIds?

Was the binding working before you started using the PartialView? Because from your previous question, I see that you had:

@Html.ListBoxFor(contact => contact.SelectedFonctionIds, Model.ListeFonctionsSelectList, new { disabled = "disabled" })

in your View (i.e., you didn't set the id attribute).