[英]MVC ajax dropdownlist url change
我是ajax的新手,需要帮助。 一旦选择了第一个下拉列表,该脚本就会填充第二个下拉列表。 使用从数据库获取数据的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>
这是我正在努力的观点
@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>
}
如果需要添加城市,请单击“提交”按钮,然后该按钮将采取正确的措施。 当我填充下拉列表时,发生了问题。 填充后,“城市”按钮不再响应。 看起来url获取堆栈位于“ / SetUp / GetStates”,并且我无法再对表单执行任何操作。
请让我知道我在做什么错以及在哪里看看? 提前致谢。
请尝试以下代码,希望对您有所帮助:
<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>
由于下拉列表必须进入标签,因此理想情况下,提交必须位于标签的最后位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.