![](/img/trans.png)
[英]Drop down list not populating in Modal Dialog when item in first drop down is changed
[英]Fill a table when a item changed in a drop down list razor mvc3
當我的下拉列表中的所選項目更改時,我想用數據庫的數據填充表。 我想用JavaScript,但我不知道如何在數據庫中搜索所選項目並填寫表格。
如果您使用的是mvc / razor,那么您的項目肯定涉及jQuery。 您可以像這樣使用Ajax調用控制器的動作:
jQuery(document).ready(function(){
$("#YourDropDownId").change(function() {
$.ajax({
url: "YourController/PopulateDetails",
data: 'id=' + $(this).val(), // Send value of the drop down change of option
dataType: 'json', // Choosing a JSON datatype
success: function(data)
{
// Variable data contains the data you get from the action method
}
});
});
});
您的方法應返回JsonResult:
public JsonResult PopulateDetails(string id)
{
// implementation here to return table data
.....
return Json(tableResultModel);
}
注意:如果您只是在尋找一種獲取所選值的方法。 然后jQuery可以再次提供幫助; 您可以使用:
$("#YourDropDownId option:selected").text();
要么
$("#YourDropDownId").val();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.