我已将2 Dropdownlist
称为国家/地区。但只有国家DDL(DropdownList)
将启用国家DDL
将被禁用。当我在状态中选择已存储在SQL
中的选项时。国家/地区DDL
应自动从DB
显示所选州的国家/地区。请帮帮我......
例如:如果我选择州为#34;德里"然后国家/地区DDL
会自动显示为"INDIA"
答案 0 :(得分:0)
根据您的要求,有两个表 States & 国家。一对多关系。状态表中 CountryId 的外键。当状态下拉列表的selectedItemChange事件触发时,有一个调用(通过jQuery或javascript)的ajax函数。
答案 1 :(得分:0)
注意:根据您的要求,您需要在用户选择州时显示国家/地区名称,然后为什么需要国家/地区的下拉列表?最好使用标签。
首先,你需要维护一个存储国家的表,它的状态如下,那么很容易取值,如果你的场景比较复杂,那么你也可以使用外键关系
id state country
1 delhi india
2 kerala india
3 California usa
然后您可以根据州
轻松选择国家/地区select distinct country from tablename where
state ='yourstate'
然后你的JS
以下代码将在您的州下拉列表的onchange事件中
然后从状态下拉列表获取值并分配给变量并通过ajax传递它
var StateName= $("#statedropdownlistid").val();// jquery solution need js then change the code
$.ajax({
type: "Get",
url:"/ControllerName/FunctionName",,
data: JSON.stringify({ StateName: StateName }),
success: function (data) {
document.getElementById("labelid").innerHTML =data;
},
dataType: "json",
contentType: 'application/json',
traditional: true
});
然后在mVC控制器
public JsonResult FunctionName(string StateName)
{
try
{
//code for getting your value from database, you can write a simple stored procedure in database and get the values here
return Json(new { data= assignthevaluefromdatabase }, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json(new { data= "Error" }, JsonRequestBehavior.AllowGet);
}
}