Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

asp.net mvc - Build an empty MVC DropdownListFor for a Cascade Sub-List

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

question from:https://stackoverflow.com/questions/13051228/build-an-empty-mvc-dropdownlistfor-for-a-cascade-sub-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...