Build an empty MVC DropdownListFor for a Cascade Sub-List

前端 未结 3 553
滥情空心
滥情空心 2021-01-31 15:40

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

The actual view:

    
相关标签:
3条回答
  • 2021-01-31 15:53

    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)
    
    0 讨论(0)
  • 2021-01-31 15:54

    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

    0 讨论(0)
  • 2021-01-31 16:18

    The following is working:

    @Html.DropDownListFor(m => m.Model_Id, **new SelectList(new List<string>()**));
    
    0 讨论(0)
提交回复
热议问题