MVC ajax下拉列表值问题

本文关键字:问题 下拉列表 ajax MVC | 更新日期: 2023-09-27 18:12:34

所以我在处理级联下拉列表时遇到了问题。每次我在第一个下拉菜单中选择一个值时,第二个下拉菜单就会被填充,但是在第二个下拉菜单的顶部会显示"第一个选择的值"。明白了吗?以下是代码。我不确定它是否追加正确,似乎在firebug上找不到任何东西。

请多多指教。

谢谢! !

视图:

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

控制器2ndlevel返回选项列表

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

MVC ajax下拉列表值问题

尝试使用jquery的live binder .

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

我必须使用live来绑定到更改事件,因为我记得…我不知道为什么。