简体   繁体   English

MVC Ajax dropdownlist网址更改

[英]MVC ajax dropdownlist url change

I am new in ajax and I need a help. 我是ajax的新手,需要帮助。 This is a script that populates second dropdownlist once first is selected. 一旦选择了第一个下拉列表,该脚本就会填充第二个下拉列表。 It works fine using GetStates action that gets data from database. 使用从数据库获取数据的GetStates操作,它可以正常工作。

<script type="text/javascript">
$(document).ready(function () {
    $("#CountriyID").change(function () {
        var abbr = $("#CountriyID").val();

        $.ajax({
            url: "/SetUp/GetStates",
            data: { countryCode: abbr },
            dataType: "json",
            type: "POST",
            error: function () {
                alert("An error occurred.");
            },
            success: function (data) {
                var items = "";
                $.each(data, function (i, item) {
                    items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
                });

                $("#StateID").html(items);
            }
        });
    });
});

</script>

Here is the view that I am working on 这是我正在努力的观点

@using (Html.BeginForm("Index", "SetUp", FormMethod.Post))
{
    <form action="" method="post">
        <input type="submit" name="action:AddCity" value="Add" />
    </form>
    <div class="editor-field">@Html.DropDownListFor(model => model.CountriyID, new SelectList(Model.Countries, "ID", "Text"))</div>
     <div class="editor-field">@Html.DropDownListFor(model => model.StateID, new SelectList(Model.States, "ID", "Text"))</div>
}

If I need to add a City I click on a submit button and it goes to correct action. 如果需要添加城市,请单击“提交”按钮,然后该按钮将采取正确的措施。 The problem happened when I populate dropdownlist. 当我填充下拉列表时,发生了问题。 After population, the City button do not respond anymore. 填充后,“城市”按钮不再响应。 It looks like the url get stack at "/SetUp/GetStates" and I cannot do any actions anymore on my form. 看起来url获取堆栈位于“ / SetUp / GetStates”,并且我无法再对表单执行任何操作。

Please Let me know what am I doing wrong and where to take a look? 请让我知道我在做什么错以及在哪里看看? Thanks in advance. 提前致谢。

try the below code hope it helps you: 请尝试以下代码,希望对您有所帮助:

<form action="" method="post">

    <div class="editor-field">@Html.DropDownListFor(model => model.CountriyID, new SelectList(Model.Countries, "ID", "Text"))</div>
     <div class="editor-field">@Html.DropDownListFor(model => model.StateID, new SelectList(Model.States, "ID", "Text"))</div>

<input type="submit" name="action:AddCity" value="Add" />
    </form>

As the drop down must come into the tags and submit must be at the last place in the tags ideally. 由于下拉列表必须进入标签,因此理想情况下,提交必须位于标签的最后位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM