简体   繁体   English

在下拉列表中更改项目时填写表格剃须刀MVC3

[英]Fill a table when a item changed in a drop down list razor mvc3

I want to fill a table with the data of my database when the selected item of my drop down list changed. 当我的下拉列表中的所选项目更改时,我想用数据库的数据填充表。 I think with a javascript but I don't know how search the selected item in my database and fill the table. 我想用JavaScript,但我不知道如何在数据库中搜索所选项目并填写表格。

If you're using mvc/razor, then your project definitely involves jQuery. 如果您使用的是mvc / razor,那么您的项目肯定涉及jQuery。 You might use an Ajax call to your controller's action like this: 您可以像这样使用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
     }
   });
  });
});

Your method should return JsonResult: 您的方法应返回JsonResult:

public JsonResult PopulateDetails(string id)
{
    // implementation here to return table data
    .....

    return Json(tableResultModel);
}

NB: If you're just looking for a way to get the selected value. 注意:如果您只是在寻找一种获取所选值的方法。 then again jQuery can help; 然后jQuery可以再次提供帮助; You can either use: 您可以使用:

$("#YourDropDownId option:selected").text();

or 要么

$("#YourDropDownId").val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM