在这里,我有一个下拉列表和一个列表框。我已使用DbContext和绑定数据为我的控制器中的Viewbag显示了下拉列表的数据。到这里它工作正常。
我的控制器..
public ActionResult Index()
{
ViewBag.StudentList = new SelectList(db.Students, "StudentID", "StudentName");
return View();
}
我的观点..
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div>
@Html.DropDownList("StudentList", "-Select-")
<br/>
@Html.ListBox("StudentMarks", new SelectList(""))
<p>
<input type="submit" value="Save" />
</p>
</div>
}
现在问题是我需要根据dropdownlist选择值中选择的值将数据绑定到列表框。最初,这里是一个空的列表框。
任何人都可以帮助我。
提前致谢
答案 0 :(得分:0)
您应该在'onchange'事件中从StudentList获取选定的密钥,然后将其传递给服务器并获取列表框值,然后使用javascript创建列表框选择的选项
答案 1 :(得分:0)
首先为下拉列表编写更改事件,例如
$('#StudentList').change(function(){
var selecteditem=$('#StudentList').val();
$.ajax({
url:'Controller/Action',
type:'POST',
data:{id:selecteditem},
cache:false,
success:function(data){
var lstItems=[];
foreach(var i=0;i<data.length;i++)
{
lstItems.push('<option value="'+data.Field1+'">data.Field2</option>');
}
$('#StudentMarks').empty();
$('#StudentMarks').append(lstItems);
}
});
});
你的行动看起来像,
[HttpPost]
public ActionResult Action(string id)
{
// get all data by the student id.
return Json(listMarks,JsonRequestBehavior.Allow.Get);
}
这适合你。