Click here to Skip to main content
15,851,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C#
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);

   }



This is my View



HTML
@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
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900