asp.net-mvc 为级联子列表构建一个空的 MVC DropdownListFor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13051228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:21:54  来源:igfitidea点击:

Build an empty MVC DropdownListFor for a Cascade Sub-List

asp.net-mvcviewhtml.dropdownlistfor

提问by Patrick

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

我想建立一个空的 Dropdownlistfor 以接收先前 Dropdownlisfor 选择的结果:

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):

实际的控制器(为了工作,我必须构建一个空的 SelectedList 但必须这样做似乎很奇怪):

   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)

有没有办法构建这样的东西:@Html.DropDownListFor(m => m.Model_Id, null)

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

并从控制器中删除 // 为模型构建下拉列表?

Thanks

谢谢

回答by Patrick

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:

找到了一个我认为最好的解决方案,因为它没有服务调用来构建空的 dropdroplist 并且它是强类型的:

@Html.DropDownListFor(m => m.Model_Id, Enumerable.Empty<SelectListItem>(), HeelpResources.DropdownlistModelFirstRecord)

回答by Maniyar Bhargav

The following is working:

以下是工作

@Html.DropDownListFor(m => m.Model_Id, **new SelectList(new List<string>()**));

回答by Chris

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

就我个人而言,我会使用一些 jQuery 和一个额外的局部视图来做到这一点。您的表单可能如下所示:

<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

然后将您的下拉列表粘贴在 GetModels 部分视图中