【问题标题】:mvc ajax dropdownlist value problemsmvc ajax 下拉列表值问题
【发布时间】:2011-10-26 19:07:17
【问题描述】:

所以我在处理级联下拉列表时遇到了问题。每次我在第一个下拉列表中选择一个值时,都会填充第二个下拉列表,但在第二个下拉列表的顶部“使用第一个中的选定值”。那有意义吗?以下是代码。我不确定它是否正确附加,似乎在萤火虫上找不到任何东西。

感谢任何建议。

谢谢!!

观点:

<script type="text/javascript">
    $(function () {
        $('#<%: ViewData.TemplateInfo.GetFullHtmlFieldId("1stLevel") %>').change(function () {
            $.ajax({
                url: '<%: Url.Action("Index","2ndLevelDetails") %>?1stLevelId=' + $(this).val(),
                success: function (data) {
                    $('#<%: ViewData.TemplateInfo.GetFullHtmlFieldId("2ndLevelId") %>').html(data);
                },
                async: false
            });
        });
    });
</script>


            <div class="dropdown">
                <%: Html.DropDownList("1stLevelDetails", new SelectList(Model.1stLevel, "1stLevelId", "1stLevelDescription"))%>
            </div>

            <div class="dropdown">
                <%: Html.DropDownListFor(model => model.2ndLevelId, new SelectList(Model.NTEESecondaryCodes, "2ndLevelId", "2ndLevelDescription", Model.2ndLevelId))%>
            </div>

返回选项列表的第二级控制器

public string Index(int 1stLevelId)
{
    var ntee = new System.Text.StringBuilder();
    foreach (2ndLevelDetails code in 2ndLevelDetails.Find2ndLevelIds(ArgentDb, 1stLevelId))
    {
        ntee.AppendFormat("<option value=\"{0}\">{1}</option>", code.2ndLevelId, code.Description);
    }

    return ntee.ToString();
}

【问题讨论】:

  • 您确定“更改”事件有效并触发?将 Ajax 代码更改为要检查的简单警报。

标签: c# asp.net-mvc ajax model-view-controller


【解决方案1】:

尝试使用 jquery 的 live binder ..

$('#<%: ViewData.TemplateInfo.GetFullHtmlFieldId("1stLevel") %>').live('change', function () {
// ... code here

自从我记得以来,我不得不使用 live 绑定到更改事件。我不知道为什么。

【讨论】:

    猜你喜欢
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多