选择不同的下拉列表项RAZOR时更改下拉选择列表

时间:2014-08-21 17:21:25

标签: jquery asp.net-mvc-4 razor selectlist

我有一个下拉列表,其中包含公司列表,我有另一个下拉列表,其中包含公司分支列表。我需要根据公司选择的项目进行分支下拉列表。因此,当用户从下拉列表中选择company1时,分支下拉列表应将列表填充到company1中的分支。我不知道该怎么做。这是我的下拉状态:

            <tr>
                <td class="search-label">
                    @Html.Label("Company: ")
                </td>
                <td>
                    @Html.DropDownList("cono", (List<SelectListItem>)ViewBag.CompanyList)
                </td>
            </tr>
            <tr>
                <td class="search-label">
                    @Html.Label("Branch: ")
                </td>
                <td>
                    @Html.DropDownList("branch", (List<SelectListItem>)ViewBag.BranchList)
                </td>
            </tr>

这是我的行动:

    [HttpGet]
    public ActionResult Default()
    {
        var currentUserCono = GetCurrentUserCono();
        //Set the Viewbags to populate the drop down menus on the view
        if (Session["cono"] != null)
        {
            currentUserCono = (int)Session["cono"];
        }
        ViewBag.CompanyList = _selectListLib.GetCompanies(currentUserCono);
        ViewBag.BranchList = Session["branch"] != null ? _selectListLib.GetBranches(currentUserCono,(string)Session["branch"]) : _selectListLib.GetBranches(currentUserCono);
        ViewBag.StatusTypeList = Session["statustype"] != null ? _selectListLib.GetStatusTypes((bool)Session["statustype"]) : _selectListLib.GetStatusTypes();

        return View();
    }

1 个答案:

答案 0 :(得分:0)

你有两种(更多,但两种主要)流行技术:

  • 使用公司ID作为参数创建一个Action(JSONResult),该参数返回分支,序列化为JSON。然后使用jQuery通过$ .getJSON绑定来检索该列表 公司下拉列表的jQuery .change()事件。那你可以 遍历该JSON列表并向分支添加选项元素 下拉列表。

或者

  • 使用公司ID作为参数创建一个Action(PartialViewResult),该参数返回包含您的分支下拉列表的部分视图,并在视图中填充相关分支。使用jQuery的.load()加载该视图,再次绑定到Company下拉列表中的jQuery .change()事件。为此,您需要在分支周围的元素上使用ID,以便将其定位为$(“#yourTDId”)。load(“/ Controller / Action /”+ $(“#cono”)。 VAL())

这是实现您所需要的最简单的两种方式。我希望这对您的应用程序有所帮​​助并祝您好运。

相关问题