我有级联下拉列表以在第二个下拉列表中显示有关第一个下拉列表中所选项目的项目,为此我已经这样做了......控制器的代码
namespace MvcSampleApplication.Controllers
{
public class CascadeListController : Controller
{
public ActionResult Index()
{
List<SelectListItem> state = new List<SelectListItem>();
state.Add(new SelectListItem { Text = "State1", Value = "State1" });
state.Add(new SelectListItem { Text = "State2", Value = "State2" });
ViewBag.StateName = new SelectList(state, "Value", "Text");
return View("BasicDrop");
}
public JsonResult Districtlist(string Id)
{
var districttype = from s in CascadingDropdowns.GetDistrictList()
where s.StateName == Id
select s;
return Json(new SelectList(districttype.ToArray(), "StateName", "DistrictName"), JsonRequestBehavior.AllowGet);
}
}
}
这是为了查看
@Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
$(function () {
$("State").change(function () {
$.getJSON('/CascadeListController/Districtlist' + $('#State').val(), function (data) {
var items = '<option>Select a District</option>';
$.each(data, function (i, districttype) {
items += "<option value = '" + districttype.Value + "'>" + districttype.Text + " </option> ";
});
$('#District').html(items);
});
});
});
</script>
@model MvcSampleApplication.Models.CascadingDropdowns
@{
ViewBag.Title = "BasicDrop";
}
<h2>BasicDrop</h2>
@using (Html.BeginForm())
{
<div>
<fieldset>
<legend>DropDownlists</legend>
@Html.Label("State")
@Html.DropDownList("State", ViewBag.StateName as SelectList,"Select A state", new {id= "State"})
@Html.Label("District");
<div class="mycombo">
<select id ="District" name ="Distrct"></select>
</div>
</fieldset>
</div>
}
当我在第一个下拉列表中选择项目时,我无法在第二个下拉列表中看到任何项目,并且我已将 div 属性用于第二个下拉列表以增加宽度,我已指定宽度,例如“70px”,但它也不起作用..
请任何人对此提出任何建议...非常感谢。