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

标签 asp.net-mvc view html.dropdownlistfor

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

实际 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>        

实际的 Controller (为了工作,我必须构建一个空的 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);
    }

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

并从 Controller 中删除//Build the Dropdownlist for the Models?

谢谢

最佳答案

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

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

关于asp.net-mvc - 为级联子列表构建一个空的 MVC DropdownListFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13051228/

相关文章:

javascript - 包括特定于 ASP.NET MVC4 View 或部分 View 的脚本

xml - SAPUI5 使用 XML 文件作为 "data-sap-ui-resourceroots"的 View ?

c# - 使用值填充下拉列表

javascript - 如何使用javascript获取下拉列表的选定值

c# - 删除 EnumDropDownListFor 框顶部的空白/空条目

regex - 正则表达式以匹配2位数字(以验证信用卡/借记卡发行号)

css - ASP.net MVC 主题更改帮助

c# - ASP.Net MVC Controller 未被调用

mysql - 如何在MySQL中找到创建 View 的SQL语句?

iphone - 是否可以确定 ViewController 是否呈现为模态?