0
我有2個下拉控件狀態和城市,其中我通過從服務器端調用Web方法填充城市下拉列表中的ajax jquery 但我沒有得到選定的值。獲取選定的值作爲插入記錄在城市下拉列表中的空值
這是我的代碼:
<asp:DropDownList ID="ddlState" runat="server" onchange=" LoadCity();"></asp:DropDownList>
<asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>
function LoadCity() {
var select = document.getElementById("<%= ddlState.ClientID %>");
var id = select.options[select.selectedIndex].value;
$.ajax({
url: "/Abc.aspx/LoadCity",
type: "POST",
contentType: "application/json; charset=utf-8",
data: '{stateId: "' + id + '"}',
contentType: "application/json",
success: function (responce) {
var html = '<option value="0">Select City</option>';
$(responce.d).each(function() {
html += '<option value="' + this.Value + '">' + this.Text + '</option>'
});
$(document.getElementById("<%= ddlCity.ClientID %>")).html(html);
}
});
[WebMethod]
public static List<ListItem> LoadCity(int stateId)
{
var data = this.ReturnCity(stateId);
return data;
}
public List<ListItem> ReturnCity(int stateId)
{
var data = context.CityMasters.Where(t => t.StateId == stateId).ToList()
.select(t=> new ListItem {Text=t.Name,Value=t.Id.ToString()}).ToList();
return data;
}
但現在當我收到市下拉列表中選擇的值,然後我得到空值:
protected void Save_Click(object sender, EventArgs e)
{
var cityId=ddlCity.SelectedValue;//Getting null here;
}
可有人告訴我這是爲什麼發生,如何獲得保存事件的城市選定的價值?
查看答案爲什麼它不工作和一個解決方法在這裏 - [SO鏈接](http://stackoverflow.com/a/1271204/2401021) – majita