Mysql 请选择“因为这是脚本中文本值中的内容。我已将您编辑的内容回滚到原始内容,因为这是我的答案所基于的。您不能完全更改问题,使所有人的答案都完全无效。您的原始代码因我的答案中的原因而不起作用(由于脚本不在$(document.ready() public cla
Mysql 请选择“因为这是脚本中文本值中的内容。我已将您编辑的内容回滚到原始内容,因为这是我的答案所基于的。您不能完全更改问题,使所有人的答案都完全无效。您的原始代码因我的答案中的原因而不起作用(由于脚本不在$(document.ready() public cla,mysql,ajax,asp.net-mvc,cascadingdropdown,Mysql,Ajax,Asp.net Mvc,Cascadingdropdown,请选择“因为这是脚本中文本值中的内容。我已将您编辑的内容回滚到原始内容,因为这是我的答案所基于的。您不能完全更改问题,使所有人的答案都完全无效。您的原始代码因我的答案中的原因而不起作用(由于脚本不在$(document.ready() public class DistrictSchoolListViewModel { public SelectList Districts { get; set; } public SelectList Schools { get; set; }
请选择“因为这是脚本中文本值中的内容。我已将您编辑的内容回滚到原始内容,因为这是我的答案所基于的。您不能完全更改问题,使所有人的答案都完全无效。您的原始代码因我的答案中的原因而不起作用(由于脚本不在
$(document.ready()
public class DistrictSchoolListViewModel
{
public SelectList Districts { get; set; }
public SelectList Schools { get; set; }
public string SelectedSchool { get; set; }
public string SelectedDistrict { get; set; }
[Required(ErrorMessage = "This code is required")]
public string DistrictCode { get; set; }
}
public ActionResult SchoolDistrictInformation()
{
var viewModel = new DistrictSchoolListViewModel()
{
Districts = new SelectList(db.Districts.ToList(), "leaID", "name"),
Schools = new SelectList(db.Schools.ToList(), "schoolID", "name")
};
return View(viewModel);
}
[HttpPost]
public ActionResult GetSchools(DistrictSchoolListViewModel model)
{
var selectedDistrict = model.SelectedDistrict;
var schools = findSchools(selectedDistrict);
IEnumerable<SelectListItem> filteredSchools =
schools.Select(m => new SelectListItem { Text = m.name, Value = m.schoolID.ToString() });
return PartialView("SchoolDistrictInformation", filteredSchools);
}
internal IQueryable<School> findSchools(string district)
{
var query = from School in db.Schools
where School.leaID.Equals(district)
select School;
return query;
}
@model Staff_Form.Models.DistrictSchoolListViewModel
<h2>Select District and School from list</h2>
<script type="text/javascript" src="/scripts/jquery-1.4.4.js"></script>
<script type="text/javascript">
$('#SelectedDistrict').on('change', function () {
$.ajax({
type: "POST",
url: 'Controller/GetSchools',
data: $(this).val(),
success: function (response) {
$('#SelectedSchool').html(response);
}
});
});
</script>
<div>
<form action="@Url.Action("StaffInformation", "Staff")" method="get">
District: @Html.DropDownListFor(m => m.SelectedDistrict, Model.Districts, "----Select----")
Security Code: @Html.TextBoxFor(m => m.DistrictCode) <br />
School: @Html.DropDownListFor(m => m.SelectedSchool, Model.Schools, "----Select----")
<input type="submit" value="Submit" />
</form>
</div>
@model System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>
@{ Layout = null;}
@foreach (var school in Model)
{
<option value="@school.Value">@school.Text</option>
}
$('#SelectedDistrict').on('change', function () {
$.ajax({
type: "POST",
url: 'Controller/GetSchools',
data: { SelectedDistrict: $(this).val() },
success: function (response) {
$('#SelectedSchool').html(response);
}
});
});