我如何在数据库uisng下拉列表中分类
我点击INVORG类别并在DEPTCODE中显示可用的内容?
using Json
[HttpPost]
public JsonResult GetDEPTCODE(string id)
{
List<SelectListItem> states = new List<SelectListItem>();
///
//I got Error Code, can you please define my wrong code and correct thanks
states = from u in _db.USERGROUPs where u.INVORG == id
select(new SelectListItem {Text =u.INVORG, Value = u.DEPTCODE});
return Json(new SelectList(states, "Value", "Text"));
}
&#13;
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//Dropdownlist Selectedchange event
$("#INVORG").change(function () {
$("#DEPTCODE").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetDEPTCODE")', // we are calling json method
dataType: 'json',
data: { id: $("#INVORG").val() },
// here we are get value of selected INVORG and passing same value
success: function (states) {
// states contains the JSON formatted list
// of states passed from the controller
$.each(states, function (i, state) {
$("#DEPTCODE").append('<option value="' + state.Value + '">' +
state.Text + '</option>');
// here we are adding option for States
});
},
error: function (ex) {
alert('Failed to retrieve states.' + ex);
}
});
return false;
})
});
</script>
&#13;
答案 0 :(得分:0)
在获取数据时将方法更改为HttpGet。
[HttpGet]
public ActionResult GetDEPTCODE(string id)
{
List<SelectListItem> items= new List<SelectListItem>();
return Json(new { data = items}, JsonRequestBehavior.AllowGet);
}
更改您的脚本如下
$("#INVORG").change(function () {
$("#DEPTCODE").empty();
$.ajax({
type: 'GET',
url: '@Url.Action("GetDEPTCODE")', // we are calling json method
dataType: 'json',
data: { id: $("#INVORG").val() },
// here we are get value of selected INVORG and passing same value
success: function (data) {
// states contains the JSON formatted list
// of states passed from the controller
$("#DEPTCODE").html('');
$.each(data["data"], function (key, value) {
$("#DEPTCODE").append($("<option>
</option>").val(value.Value).html(value.Text));
});
},
error: function (ex) {
alert('Failed to retrieve states.' + ex);
}
});
return false;
})