Build an empty MVC DropdownListFor for a Cascade Sub-List
Asked Answered
S

3

40

I would like to build an empty Dropdownlistfor to received the results of a previous Dropdownlisfor selection:

The actual view:

    <div id="makes">
        @Html.DropDownListFor(m => m.Make_Id, Model.MakeList, HeelpResources.DropdownlistMakeFirstRecord)
    </div>
    <div id="models">
        @Html.DropDownListFor(m => m.Model_Id, Model.ModelList, HeelpResources.DropdownlistModelFirstRecord)
    </div>        

The actual Controller (to work I had to build an empty SelectedList but it seems strange to have to do this):

   public virtual ActionResult Create()
    {
        // Build the Dropdownlist for the Makes
        var makesDto = _makeService.ListAllMakes();
        var makesViewModel = Mapper.Map<IList<MakeDto>, IList<MakeViewModel>>(makesDto);

        // Build the Dropdownlist for the Models
        var makeId = -1;
        var modelsDto = _modelService.ListModelByMake(makeId);
        var modelsViewModel = Mapper.Map<IList<ModelDto>, IList<ModelViewModel>>(modelsDto);

        // Build the ViewModel to return to the View
        CreateAdViewModel viewModel = new CreateAdViewModel();
        viewModel.MakeList = new SelectList(makesViewModel, "ID", "Name");
        viewModel.ModelList = new SelectList(modelsViewModel, "ID", "Name"); 

        return View(viewModel);
    }

Is there a way to build something like this: @Html.DropDownListFor(m => m.Model_Id, null)

And remove the // Build the Dropdownlist for the Models from the controller?

Thanks

Shriek answered 24/10, 2012 at 14:23 Comment(0)
S
125

Found a solution that I think is the best because it as no service call to build the dropdroplist empty and it's strongly typed:

@Html.DropDownListFor(m => m.Model_Id, Enumerable.Empty<SelectListItem>(), HeelpResources.DropdownlistModelFirstRecord)
Shriek answered 25/10, 2012 at 13:30 Comment(3)
It would be good if you marked this as the answer to your own question.Unhesitating
what is the use of "HeelpResources.DropdownlistModelFirstRecord"?Streetcar
It's the text that you want to put in the first option like "Please Select an option...", but based in a Resource String so you can present this text based in the location's language website (French/Portuguese/English)Shriek
C
2

The following is working:

@Html.DropDownListFor(m => m.Model_Id, **new SelectList(new List<string>()**));
Conglutinate answered 19/4, 2016 at 9:10 Comment(0)
P
0

Personally I would do this with a bit of jQuery and an additional partial view. Your form could look like this:

<div id="makes">
        @Html.DropDownListFor(m => m.Make_Id, Model.MakeList, HeelpResources.DropdownlistMakeFirstRecord)
</div>
<div id="models">

</div>

<script type="text/javascript">
$(function(){
   $("#Make_Id").change(function(){
       $("#models").load("/Controller_Name/GetModels/" + this.val());
   }
});
</script>  

and then in your controller:

public ActionResult GetModels(int id)
{
   ViewBag.DdlModels = new SelectList(rep.GetModelsForCar(id), "Id", "Name");
   return PartialView();
}

and then just stick your drop down list in the GetModels partial view

Pantheas answered 24/10, 2012 at 15:43 Comment(1)
And the Model dropdown gets strongly typed in the view coming the partial view? It will pass the Model_Id selected value from the dropdown to the controller inside the viewmodel?Shriek

© 2022 - 2024 — McMap. All rights reserved.