Asp.net mvc 4 使用ajax将@html.dropdownlist从其他@html.dropdownlist填充到同一个表中
我正在使用dropdownlists开发一些搜索/过滤功能 问题是如何通过在第一个ddl中选择项来填充第二个dropdownlist 如果所有数据都保存在ont表中。 我有一个目录,上面有制造商、型号和年份。 当我在第一个ddl中选择一个制造商时,第二个将由该制造商的型号填充 控制器中的代码:Asp.net mvc 4 使用ajax将@html.dropdownlist从其他@html.dropdownlist填充到同一个表中,asp.net-mvc-4,html-select,Asp.net Mvc 4,Html Select,我正在使用dropdownlists开发一些搜索/过滤功能 问题是如何通过在第一个ddl中选择项来填充第二个dropdownlist 如果所有数据都保存在ont表中。 我有一个目录,上面有制造商、型号和年份。 当我在第一个ddl中选择一个制造商时,第二个将由该制造商的型号填充 控制器中的代码: public ActionResult Search(string manufacturer, string carModel, string searchString, string gear)
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);
}
}
public ActionResult搜索(字符串制造商、字符串carModel、字符串搜索字符串、字符串齿轮)
{
使用(CarTypesLogic逻辑=新的CarTypesLogic())
{
IEnumerable allCarTypes=logic.GetAllCarTypes();
var manufacturerList=新列表();
var modelList=新列表();
var gearList=新列表();
var foundManufacturer=来自所有类型中的制造商
制造商订购
选择制造商;
var foundModels=来自所有类型中的模型
orderby模型
选择模型。模型;
var foundGears=来自所有类型的变速箱
选择变速器档位;
AddRange(foundManufacturer.Distinct());
AddRange(foundModels.Distinct());
gearList.AddRange(foundGears.Distinct());
ViewBag.Manufacturer=新选择列表(manufacturerList);
ViewBag.Gear=新选择列表(gearList);
ViewBag.carModel=新选择列表(模型列表);
var foundCars=从所有cartypes中的car选择car;
如果(!String.IsNullOrEmpty(制造商))
{
foundCars=foundCars.Where(car=>car.Manufacturer==Manufacturer);
}
如果(!String.IsNullOrEmpty(齿轮))
{
foundCars=foundCars.Where(car=>car.Gear==Gear);
}
如果(!String.IsNullOrEmpty(searchString))
{
foundCars=foundCars.Where(car=>car.Manufacturer.ToLower().Contains(searchString.ToLower());
}
如果(!String.IsNullOrEmpty(carModel))
{
foundCars=foundCars.Where(car=>car.Model==carModel);
}
返回视图(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>
}
@使用(Html.BeginForm())
{
制造商:@Html.DropDownList(“制造商”,String.Empty)
模型:@Html.DropDownList(“carModel”,String.Empty)
Gear:@Html.DropDownList(“Gear”,String.Empty)
自由类型:@Html.TextBox(“搜索字符串”)
}
您需要使用级联DropDownList
以下是几个例子: