在运行时在视图中获取DropDownList的选定值?

时间:2013-07-29 03:55:52

标签: asp.net-mvc html-select selectlist

我有两个dropDownLists:

 @Html.DropDownListFor(model => model.Organization, (SelectList)ViewBag.Organizations)
 @Html.DropDownListFor(model=>model.Doctor, (SelectList)ViewBag.Doctors)

selectList从控制器传递

        List<IOkpolu> orgs = _unitOfWork.Organizations.ToList();
        ViewBag.Organizations = new SelectList(orgs, "Code", "Name");

        List<IDoctor> docs = _unitOfWork.Doctors.GetAll().ToList();
        ViewBag.Doctors = new SelectList(docs, "Code", "Name");

我想在运行时在第一个DropDownList中选择基于组织代码的值加载第二个DropDownList(Doctors)。 对于医生,我有一种方法可以让医生从组织中获取:

_unitOfWork.Doctors.GetByOrganization(string organizationCode)

如何在运行时获取第一个DropDownList中选择的值并将其传递给第二个DropDownList?

提前多多感谢!

1 个答案:

答案 0 :(得分:0)

在您的控制器中,在运行时使用默认组织代码(Doctors列表中的第一项)加载orgs列表。

    List<IOkpolu> orgs = _unitOfWork.Organizations.ToList();
    ViewBag.Organizations = new SelectList(orgs, "Code", "Name");

    model.Organization = orgs[0];

    List<IDoctor> docs = _unitOfWork.Doctors.GetByOrganization(model.Organization).ToList();
    ViewBag.Doctors = new SelectList(docs, "Code", "Name");