如何创建级联下拉列表?

时间:2014-10-04 08:14:53

标签: c# asp.net

我是MVC的新手,需要有关级联下拉列表的帮助。目的是记录场地中有故障设备的故障。我有两张桌子:" Venue"和" VenueEquipment"。第一个下拉列表应该是一个场地列表,一旦从列表中选择一个场地,它应该填充场地中各种设备类型的另一个下拉列表。我假设用户只能用一种类型的设备记录故障。

我的" FaultsController"中的代码是:

    // GET: Faults/Create
    public ActionResult Create()
    {
        ViewBag.UserID = new SelectList(db.UserTables, "UserID", "UserEMailAddress");
        ViewBag.VenueNum = new SelectList(db.VenueEquipmentTables, "VenueEquipmentID",                          "VenueNum");
        ViewBag.VenueEquipmentID = new SelectList(db.VenueEquipmentTables, "VenueEquipmentID", "VenueEquipmentType");

        return View();
    }

我认为的代码是:

    <div class="form-group">
        @Html.LabelFor(model => model.VenueEquipmentID, "Venue Number", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("VenueNum", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.VenueEquipmentID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.VenueEquipmentID, "Venue Equipment Type", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("VenueEquipmentID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.VenueEquipmentID, "", new { @class = "text-danger" })
        </div>
    </div> 

我的模型 - &#34;故障表&#34;如下:

public partial class FaultTable
{

    public int UserID { get; set; }
    public int VenueEquipmentID { get; set; }

    public virtual UserTable UserTable { get; set; }
    public virtual VenueEquipmentTable VenueEquipmentTable { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您需要创建单独的操作,返回依赖下拉列表的JSON,然后调用change事件并在该事件内部通过jQuery调用该操作并绑定另一个下拉列表。

一些例子

  1. http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/
  2. http://www.codeproject.com/Articles/730953/Cascading-Dropdown-List-With-MVC-LINQ-to-SQL-and-A