我正在使用下拉列表来处理一些搜索/过滤功能 问题是如何通过在第一个ddl中选择项目来填充第二个下拉列表 如果所有数据都保存在ont表中。 我有CarType表,有制造商,型号和年份。 当我在第一个ddl选择制造商时,第二个将由该制造商的型号填充。
我在控制器中的代码:
public ActionResult Search(string manufacturer, string carModel, string searchString, string gear)
{
using (CarTypesLogic logic = new CarTypesLogic())
{
IEnumerable<CarType> allCarTypes = logic.GetAllCarTypes();
var manufacturerList = new List<string>();
var modelList = new List<string>();
var gearList = new List<string>();
var foundManufacturer = from maker in allCarTypes
orderby maker.Manufacturer
select maker.Manufacturer;
var foundModels = from model in allCarTypes
orderby model.Model
select model.Model;
var foundGears = from transmission in allCarTypes
select transmission.Gear;
manufacturerList.AddRange(foundManufacturer.Distinct());
modelList.AddRange(foundModels.Distinct());
gearList.AddRange(foundGears.Distinct());
ViewBag.Manufacturer = new SelectList(manufacturerList);
ViewBag.Gear = new SelectList(gearList);
ViewBag.carModel = new SelectList(modelList);
var foundCars = from car in allCarTypes select car;
if (!String.IsNullOrEmpty(manufacturer))
{
foundCars = foundCars.Where(car => car.Manufacturer == manufacturer);
}
if (!String.IsNullOrEmpty(gear))
{
foundCars = foundCars.Where(car => car.Gear == gear);
}
if (!String.IsNullOrEmpty(searchString))
{
foundCars = foundCars.Where(car => car.Manufacturer.ToLower().Contains(searchString.ToLower()));
}
if (!String.IsNullOrEmpty(carModel))
{
foundCars = foundCars.Where(car => car.Model == carModel);
}
return View(foundCars);
}
}
我的cshtml代码:
@using (Html.BeginForm())
{
<p>
Manufacturer: @Html.DropDownList("manufacturer", String.Empty)
Model: @Html.DropDownList("carModel",String.Empty)
Gear: @Html.DropDownList("gear", String.Empty)
Free Type: @Html.TextBox("SearchString")
<br />
<input type="submit" value="Search"/>
</p>
}
答案 0 :(得分:0)
您需要使用级联DropDownList 。
这里有几个例子:
http://www.itorian.com/2013/02/cascading-dropdownlist-in-aspnet-mvc.html