【问题标题】:How to add an object to a listbox by clicking on checkbox?如何通过单击复选框将对象添加到列表框?
【发布时间】:2013-08-08 21:19:43
【问题描述】:

我有一个带有复选框列表和列表框的表单。

我需要当您选中或取消选中复选框时,它会通过添加或删除列表中的对象来修改列表框中的列表。看起来是这样的:

这是表单中该部分的代码:

<div class="row-fluid">
                                <div class="span3">
                                     <label>Fonction(s):</label>
                                </div>
                                <div class="span9">                                        
                                    @Html.ListBoxFor(contact => contact.SelectedFonctionIds, Model.ListeFonctionsSelectList, new { disabled = "disabled" })                                
                                </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 id="@fonction.LibelleFonction" onclick="javascript:AjouterFonction();" value="@fonction.IdFonction" />@fonction.LibelleFonction <br />
                                            }
                                    </div> 
                                </div>
                            </div>

有人知道怎么做吗?

我在考虑 Ajax,但问题是它是一个强类型视图,所有数据都与一个模型相关联。

【问题讨论】:

    标签: asp.net-mvc-4 checkbox listbox


    【解决方案1】:

    不确定这是否是一个好的解决方案,但如果您将列表框放入部分视图(也为强类型),然后您的 AjouterFonction() JavaScript 将对 Action 进行 Ajax 调用,该操作返回包含更新的 PartialView该列表框的版本?然后使用 jQuery 将之前的列表框替换为新的列表框。

    控制器动作:

    public PartialViewResult ActionName(<whatever you need here>)
    {
        // Whatever logic you may need: which item was clicked, what should be added to the model object, etc....
        return PartialView("ListBoxPartial", yourModelObject);
    }
    

    局部视图 ListBoxPartial:

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

    在您的视图中,而不是

    <div class="span9">                                        
        @Html.ListBoxFor(contact => contact.SelectedFonctionIds, Model.ListeFonctionsSelectList, new { disabled = "disabled" })                                
    </div>
    

    你会:

    <div class="span9">                                        
       @Html.Partial("ListBoxPartial", Model)                        
    </div>
    

    你的 AjouterFonction(),在接收到新的列表框后,AjouterFonction() 会搜索类为“span9”的 div 并将其内容替换为接收到的数据。

    可能有更好的方法来解决这个问题,但这是我想到的第一件事。

    【讨论】:

    • 嗯,为什么不呢,有趣,我会检查一下并给你反馈
    • 好的,最后告诉我你是怎么做到的。
    • 它可以工作,但最后,当我提交整个表单时,selectedFonctionIds 没有与联系人关联......
    • 绑定不正确。你能告诉我生成的 html 标记吗?另外,看看这是否有帮助:stackoverflow.com/questions/7966201/…
    • 我已经尝试了他们在链接中所说的内容,但没有发生任何事情。我向您展示部分视图的生成标记:link
    猜你喜欢
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    相关资源
    最近更新 更多