级联DropDownList对于选定的值插入问题 [英] Cascading DropDownListFor selected values insertion issue
问题描述
我有两个国家和城市下拉菜单.
I have two dropdowns for Country and City.
<div class="editor-field">
@Html.DropDownListFor(Model => Model.Country, new SelectList(ViewBag.Countries as System.Collections.IEnumerable, "CountryCode", "CountryName"), "Select country", new { id = "ddlCountry" })
@Html.ValidationMessageFor(model => model.Country)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.City, "City")
</div>
<div class="editor-field">
@Html.DropDownListFor(Model => Model.City, new SelectList(Enumerable.Empty<SelectListItem>(), "CityId", "CityName"), "select a city", new { id = "ddlCity" })
@Html.ValidationMessageFor(model => model.City)
</div>
和javascript函数
and the javascript function
<script type="text/javascript">
$(document).ready(function () {
$("#ddlCountry").change(function () {
var idCity = $(this).val();
$.getJSON("/Applicant/GetCityByCountry", { id: idCity },
function (CountryData) {
var select = $("#ddlCity");
select.empty();
// select.append($('<option/>',{ value: 0, text: "Select Major" }));
$.each(CountryData, function (index,itemData) { //
select.append($('<option/>', { value: itemData.value, text: itemData.Text }));
});
});
});
});
</script>
当我选择一个国家/地区时,它将填充城市下拉列表,但是当我尝试插入时,则会出现错误.
INSERT语句与FOREIGN KEY约束"Applicant_City"冲突.数据库"EngageDBase"
的表"dbo.City"
的列''CityId''
中发生了冲突.
该声明已终止.
快速监视总是显示国家代码和等于"0"的城市标识,这不在城市表中.
国家和城市模型在视图中为空.
不知道该怎么办,请帮忙.
[代码]块Edited
When I select a country, it populates the city dropdown, but when I am trying to insert, it gives error.
The INSERT statement conflicted with the FOREIGN KEY constraint "Applicant_City". The conflict occurred in database "EngageDBase"
, table "dbo.City"
, column ''CityId''
.
The statement has been terminated.
Quick watch is always showing countrycode and cityid equal to ''0'' which is not in city table.
Country and city model are null in the view.
Don''t know what to do please help.
[Code] block Edited
推荐答案
(文档).ready(
(document).ready(function () {
( #ddlCountry").change(功能(){ var idCity =
("#ddlCountry").change(function () { var idCity =
( this ).val();
这篇关于级联DropDownList对于选定的值插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!