Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 视图未与模型正确绑定_Asp.net Mvc 4_Binding_Partial Views - Fatal编程技术网

Asp.net mvc 4 视图未与模型正确绑定

Asp.net mvc 4 视图未与模型正确绑定,asp.net-mvc-4,binding,partial-views,Asp.net Mvc 4,Binding,Partial Views,我能弄明白为什么它没有约束力。因此,我有一个表单,其中列表框位于局部视图中,每次单击复选框填充列表框时,我都会重新加载该表单 表单的my ModelView代码为: <div class="row-fluid"> <div class="span3"> <label>F

我能弄明白为什么它没有约束力。因此,我有一个表单,其中列表框位于局部视图中,每次单击复选框填充列表框时,我都会重新加载该表单

表单的my ModelView代码为:

                            <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>
与该视图关联的模型如下所示:

    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;
    }
private List\u选择的功能ID;
公共列表选定的功能ID
{
得到
{
返回_selectedFonctionIds??新列表();
}
设置
{
_SelectedFonctionId=值;
}
}
public List listfontions=new List();
公共多选列表列表功能选择列表
{
得到
{
返回新的多选列表(
听众,
“IdFonction”,//dataValueField
“LibelleFonction”//dataTextField
);
}
}
公共联系人(){}
公共联系人(列出ListFontions,列出SelectedFontionId)
{
this.listFontions=listFontions;
this.SelectedFonctionIds=SelectedFonctionIds;
}
公共联系人(int-idContact、string-nom、string-prenom、string-email、string-telephoneFixe、string-telephonePort){
this.IdContact=IdContact;
这个.Nom=Nom;
这个。Prenom=Prenom;
this.Email=电子邮件;
this.TelephoneFixe=TelephoneFixe;
this.TelephonePort=电话端口;
}
公共联系人(int-id-Contact、string-nom、string-prenom、List-selectedFonctionIds、List-listedfonctions、string-email、string-telephoneFixe、string-telephonePort)
{
this.IdContact=IdContact;
这个.Nom=Nom;
这个。Prenom=Prenom;
this.SelectedFonctionIds=SelectedFonctionIds;
this.listFontions=listFontions;
this.Email=电子邮件;
this.TelephoneFixe=TelephoneFixe;
this.TelephonePort=电话端口;
}

但是局部视图的列表框不与模型绑定。我很了解列表框中的其他信息,但不了解这些信息。有人有主意了?

为什么要在这里强制输入列表框的id:

@Html.ListBoxFor(contact => contact.SelectedFonctionIds, 
   new MultiSelectList(Model.ListeFonctions, "IdFonction", "LibelleFonction"), 
   new { disabled = "disabled", **id="idFonctions"** })
ListBoxFor helper应该为您生成ListBox的id,该id应该与它应该绑定的属性相同。不应该选择FonctionId吗


在您开始使用PartialView之前,绑定是否正常工作?因为从你之前的问题中,我看到你有:

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

在您的视图中(即,您没有设置id属性)。

您可以将问题更改为较小的值吗?这么大的问题让我们无法理解。您只需要列表值,就可以剪切与问题不对应的其他信息,现在就足够了吗?对不起,我尝试了一些内容,但忘记删除它,您是对的,生成的id是SelectedFonctionID。我编辑了帖子在您开始使用PartialView之前绑定是否工作?我不知道,我没有尝试,因为我需要通过单击复选框来启动向列表框添加项目的操作,但让我检查它是否绑定,没有绑定。我刚试过,它也没有绑定,所以问题不是来自PartialView YUP,你使用列表框的方式有问题。不幸的是,我以前从未使用过列表框,所以我不能在这方面提供很好的帮助。试着研究一下它们应该如何工作(例如)。我现在很忙,可能会晚些时候再看,祝你好运!
@Html.ListBoxFor(contact => contact.SelectedFonctionIds, Model.ListeFonctionsSelectList, new { disabled = "disabled" })