@ { ViewBag.Title =“索引”; Layout = null; }
<h2>Index</h2>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Country State City Bind </title>
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function ()
{
//******* code for state bind with countryid…………..
$("#ddlcountry").change(function () {
//debugger;
//$(this).closest('tr').remove();
var v2 = $(this).val();
$.get("DropdownTest/StateBind", { country: v2 }, function (data) {
$("#state").empty();
var v = "<option>Select</option>";
$.each(data, function (i, v1) {
v += "<option value='" + v1.Value + "'>" + v1.Text + "</option>";
});
$("#state").html(v);
});
});
//******* code for city bind with stated……………………
$("#state").change(function () {
var v2 = $(this).val();
$.get("DropdownTest/CityBind", { state: v2 }, function (data) {
$("#city").empty();
var v = "<option>Select</option>";
$.each(data, function (i, v1) {
v += "<option value='" + v1.Value + "'>" + v1.Text + "</option>";
});
$("#city").html(v);
});
});
});
</script>
</head>
<body>
<div>
<table>
<tr><td colspan="2"><font color="Red">Bind Json Result to DropDownlist in MVC4 Country State City Concept</font></td></tr>
<tr><td>Country Name</td><td>@Html.DropDownList("ddlcountry", "Select" )</td></tr>
<tr><td>State Name</td><td><select id="state"><option>Select</option></select></td></tr>
<tr><td>City Name</td><td><select id="city"><option>Select</option></select></td></tr>
</table>
</div>
</body>
</html>
在这里输入代码
使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.Mvc;
使用MVCDropdownlist.Dal;
命名空间MVCDropdownlist.Controllers { 公共类DropdownTestController:Controller { // // GET:/ DropdownTest /
harshalEntities ctx = new Dal.harshalEntities();
public ActionResult Index()
{
ViewBag.ddlcountry = CountryBind();
return View();
}
//code for bind Country Name...........................
public List<SelectListItem> CountryBind()
{
List<SelectListItem> li = new List<SelectListItem>();
foreach (var v in ctx.Countries)
{
li.Add(new SelectListItem { Text = v.CountryName, Value = v.CountryId.ToString() });
}
//ViewBag.ddlcountry = li;
//return View();
return li;
}
//code for bind State Name...........................
public JsonResult StateBind(string country)
{
int id = Convert.ToInt32(country);
var v = ctx.States.Where(m => m.CountryId == id).Select(m => new { Text = m.StateName, Value = m.StateId });
return Json(v, JsonRequestBehavior.AllowGet);
}
//code for bind City Name...........................
public JsonResult CityBind(string state)
{
int id = Convert.ToInt32(state);
var v = ctx.Cities.Where(m => m.StateId == id).Select(m => new { Text = m.CityName, Value = m.CityId });
return Json(v, JsonRequestBehavior.AllowGet);
}
}
}
我的功能不在里面获取方法