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

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 为Cascade子列表构建一个空的MVC DropdownListFor前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想建立一个空的Dropdownlistfor接收以前的Dropdownlis选择的结果:

实际看法:

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

实际的控制器(要工作,我必须构建一个空的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,"Name"); 

        return View(viewmodel);
    }

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

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

谢谢

解决方法

找到一个我认为是最好的解决方案,因为它没有服务调用来构建dropdroplist为空,它是强类型的:
@Html.DropDownListFor(m => m.Model_Id,Enumerable.Empty<SelectListItem>(),HeelpResources.DropdownlistModelFirstRecord)
原文链接:https://www.f2er.com/aspnet/253909.html

猜你在找的asp.Net相关文章