级联下拉列表-asp.net mvc

ldioqlga  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(417)

我使用asp.NETMVC,我有一个数据库的odbc连接,并从控制器中检索了两个下拉列表以使用查询查看。
这两个下拉列表是:

@Html.DropDownListFor(model => model.storageLocation, new SelectList(Model.locationGroupDD, "storageLocation", "storageLocation"), "Choose Location Group", new { @id = "storageLocation", @class = "dropdown1" })

@Html.DropDownListFor(model => model.storageLocationList, new SelectList(Model.locationDD,"storageLocationList","storageLocationList"), "Choose Location", new { @id = "storageLocationListDropDown", @class = "dropdown1" })

我是jquery的新手,不知道如何编写脚本。但是,我在网上找到了这个脚本,并尝试使用以下内容进行必要的更改,但我确实不知道如何修改/继续。感谢您的帮助!谢谢您。
以下是我用于从数据库检索数据的查询:

For drop downlist 1: select abc from xyz;
For drop downlist 2: select pqr from lmn where abc = "some value";

我想把从下拉列表1中选择的值传递给控制器,以执行对第二个下拉列表的查询。

k4emjkb1

k4emjkb11#

请按照以下步骤在asp.net mvc中创建级联dropdownlist:

1在控制器中:

public class YourControlleNameController : Controller
{
   public ActionResult Create()
   {
        var LocationGroupDDList = _dbContext.LocationGroupDD.Select(lgdd => 
                                             new { lgdd.LocationGroupDDId, lgdd.LocationGroupDDName }).ToList();

        ViewBag.LocationGroupDDSelectList = new SelectList(LocationGroupDDList, "LocationGroupDDId", "LocationGroupDDName");

        ViewBag.LocationDDListSelectList = new SelectList(new List<LocationDD>(), "LocationDDId", "LocationDDName");

        return View();
   }

  [HttpPost]
  public ActionResult Create(YourModel model, string LocationGroupDDId)
  {
        if (ModelState.IsValid)
        {
            // Do necessary staff here
        }
        var LocationGroupDDList = _dbContext.LocationGroupDD.Select(lgdd => 
                                             new { lgdd.LocationGroupDDId, lgdd.LocationGroupDDName }).ToList();

        ViewBag.LocationGroupDDSelectList = new SelectList(LocationGroupDDList, "LocationGroupDDId", "LocationGroupDDName",LocationGroupDDId);

        var LocationDDList = _dbContext.LocationDD.Where(ldd => ldd.LocationGroupDDId == LocationGroupDDId).Select(ldd => new {ldd.LocationDDId, ldd.LocationDDName}).ToList();

        ViewBag.LocationDDListSelectList = new SelectList(LocationDDList, "LocationDDId", "LocationDDName",model.LocationDDId);

        return View();
  }

   public JsonResult GetLocationDDByLocationGroupDD(string LocationGroupDDId)
   {
        var LocationDDList = _dbContext.LocationDD.Where(ldd => ldd.LocationGroupDDId == LocationGroupDDId)
                                                  .Select(ldd => new {ldd.LocationDDId, ldd.LocationDDName}).ToList();

        return Json(LocationDDList, JsonRequestBehavior.AllowGet);
   }

}

2在视图中:

<div class="form-group">
    @Html.Label("LocationGroupDD", "Location GroupDD Name", htmlAttributes: new { @class = "control-label" })
    @Html.DropDownList("LocationGroupDDId", ViewBag.LocationGroupDDSelectList as SelectList, "Select Location GroupDD", htmlAttributes: new { @class = "form-control",  @id = "LocationGroupDD" })

</div>
<div class="form-group">
    @Html.Label("LocationDD", "LocationDD Name", htmlAttributes: new { @class = "control-label" })
    @Html.DropDownList("LocationDDId", ViewBag.LocationDDListSelectList as SelectList, "Select LocationDD", htmlAttributes: new { @class = "form-control", @disabled = "disabled", @id = "LocationDD" })                   
</div>

三。视图中的jquery:

@section Scripts {
    <script type="text/javascript">
      $(document).on('change','#LocationGroupDD', function(){
          var LocationGroupDDId = $(this).val();
          $('#LocationDD').empty();
          if (LocationGroupDDId) {
            $.ajax({
                type: "GET",
                url: '@Url.Action("GetLocationDDByLocationGroupDD", "YourControlleName")',
                data: { LocationGroupDDId: LocationGroupDDId},
                success: function(data) {
                    if (data.length > 0) {
                        $('#LocationDD').prop("disabled", false);
                        $('#LocationDD').append($("<option>").val("").text("Select LocationDD"));
                        $(data).each(function(index, item) {
                            $('#LocationDD').append($("<option>").val(item.LocationDDId).text(item.LocationDDName));
                        });
                    } else {
                      $('#LocationDD').append($("<option>").val("").text("LocationDD List Is Empty"));
                    }
                }
            });
        } else {
            $('#LocationDD').prop("disabled", true);
            $('#LocationDD').append($("<option>").val("").text("Select Location GroupDD First"));
        }
      });
    </script>
}

希望这能解决你的问题!

相关问题