0
我在我的視圖'國家'和'區'有兩個DropDownList。我使用下面的代碼來填充'國家'DDL。<select...> DropDownList有沒有項目可以選擇回發或驗證失敗
控制器: -
var districts = (from r in db.Districts
select new { r.DistrictName });
ViewBag.DistrictName = new SelectList(districts, "DistrictName", "DistrictName");
查看: -
@Html.DropDownList("State", ViewBag.StateName as SelectList, "Select a State")
現在,每當拿起一個國家一個JSON調用填充 '區' DDL。
控制器: -
[HttpGet]
public JsonResult DistrictList(string Id)
{
var districts = (from d in db.Districts
where d.State.StateName == Id
select new { d.DistrictName });
return Json(new SelectList(districts, "DistrictName", "DistrictName"), JsonRequestBehavior.AllowGet);
}
查看: -
<select id="District" name="District"></select>
<script type="text/jscript">
$(function() {
$('#State').change(function() {
$.getJSON('/Application/DistrictList/' + $('#State').val(), function (data) {
var items = '<option>Select a District</option>';
$.each(data, function (i, districts) {
items += "<option value='" + districts.Text + "'>" + districts.Text + "</option>";
});
$('#District').html(items);
});
});
});
</script>
,它工作正常,但問題是,每當任何驗證失敗, '區' DDL失去其數據。再次,我需要在'狀態'中做出新的選擇,然後'區'從Json resutl獲得數據。如何處理這個?