Abraham Lukwesa

Abraham Lukwesa

  • NA
  • 13
  • 3.3k

How to filter Dropdowns and filter a table in ASp.net

Jul 20 2015 6:34 AM
Hi All,

I have a got database of vehicles. The database contains properties such as manufacturers, models, engine size and fueltype.

Now in my project(View) I am displaying this data in a table and using dropdowns to filter the table. At the moment i have got a manufacturer dropdown and a car model dropdown. when a user selects the dropdowns they click a search button that that filters the according to the drop downs. This is how my project is working at the moment.

Is there anyway i could filter the dropdowns? what i mean by this is that is there a way i could select the manufacturer dropdown and this then filters all car Models according to the slected manufacturer .. so instead of showing all the models just show the selected manufacturer dropdowns?
 
 
 

Controller
public ActionResult BrowseCars( string manufacturer, string model)
{
ViewBag.manufacturer = (from m in _carCatalogue.CarsTable
select m.Name).Distinct();
ViewBag.model = (from v in _carCatalogue.CarsTable
select v.Vehicle).Distinct();
var car = from t in _carCatalogue.CarsTable
orderby t.Name
where t.Name == manufacturer || manufacturer == null || manufacturer == ""
where t.Vehicle == model || model == null || model == ""
select t;
return PartialView("_BrowseCars", car);
}
 
 
View
 
@model IEnumerable<Model.CarsTable>
<p> Use the relevant dropdowns below to find the vehicle </p>
<p>
@using (Html.BeginForm())
{
<text> Manufacturer </text>@Html.DropDownList("manufacturer", new SelectList(ViewBag.manufacturer))
<text> Model </text>@Html.DropDownList("model", new SelectList(ViewBag.model))
<input type="submit" value="search"/>
}
</p>
<div>
<table>
<tr>
<th>
Mnaufacturer
</th>
<th>
Model
</th>
<th>
Engine Size
</th>
<th>
BHP
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Vehicle)
</td>
<td>
@Html.DisplayFor(modelItem => item.EngineSize)
</td>
<td>
@Html.DisplayFor( modelItem => item.Bhp)
</td>
}
</table>
 
 Thank you for your time