@Html.DropDownList("GroupCode",(IEnumerable<SelectListItem>)ViewBag.GroupCode,"-- Please Select --", new{@id="groupcd"})
DropDownList选择组并将选中的值回发给JSON以获取客户端代码列表并放入另一个DropDownList
$(function() {
$("#groupcd").change(function () {
var selectedValue = $("#groupcd").val();
$.post('@Url.Action("getClientCD", "OutstandingClaim")', { selection: selectedValue }, function (data) {
$("#clientcd").html(data);
});
});
});
Javascript获取所选值并发布到操作
public JsonResult getClientCD(string selection)
{
var data = db.Common_CustMas.Where(c => c.Group == selection)
.ToList().Select(x => string.Format("{0}--{1}", x.CUSTCODE, x.CUSTNAME));
ViewBag.ClientCode = data;
return Json(data, JsonRequestBehavior.AllowGet);
}
回发到控制器后,查询列表并传回
@Html.DropDownList("ClientCD", (IEnumerable<SelectListItem>) ViewBag.ClientCode, "--Please Select --", new{@id="clientcd"})
查询有效,但没有数据传回ViewBag客户端代码。任何人都知道什么问题以及如何解决这个问题?
答案 0 :(得分:0)
你可以这样做
$('#CityID').change(function () {
var cityid = $(this).val();
$.ajax({
url: '@Url.Action("PopulateDistrictsList ","Controller")',
data: { CityID: cityid },
datatype: "json",
success: function (data) {
var ddldist = $('#DistrictID');
$.each(data, function (val, text) {
ddldist.append(
$('<option></option>').val(val).html(text)
);
});
},
})
});
答案 1 :(得分:0)
最后用代码
解决了$(function() {
$("#groupcd").change(function () {
var selectedValue = $("#groupcd").val();
$.getJSON('@Url.Action("getClientCD", "OutstandingClaim")', { selection: selectedValue }, function (result) {
var ddl = $('#clientcd');
ddl.empty();
$(result).each(function () {
ddl.append(
$('<option/>').html(this.Text)
);
});
});
});
});
控制器
public JsonResult getClientCD(string selection)
{
var data = (from a in db.Common_CustMas
where a.Group == selection
select new {Text = ( a.CUSTCODE + "-" + a.CUSTNAME)}).Distinct().ToList();
return Json(data, JsonRequestBehavior.AllowGet);
}
下拉列表
@Html.DropDownList("GroupCode",(IEnumerable<SelectListItem>)ViewBag.GroupCode,"-- Please Select --", new{@id="groupcd"})
@Html.DropDownList("ClientCD", new SelectList(Enumerable.Empty<SelectListItem>()),new{@id="clientcd"})