How can I reuse a DropDownList in several views with .NET MVC

后端 未结 13 1083
感动是毒
感动是毒 2020-12-13 15:53

Several views from my project have the same dropdownlist...

So, in the ViewModel from that view I have :

public IEnumerable Fo         


        
13条回答
  •  感情败类
    2020-12-13 16:15

    If you really don't want to duplicate the code, place the code from the controllers into a helper class, and render the dropdown within a shared view (like _Layout.cshtml) that you'd then have to implement into your views by RenderPartial.

    Create a partial view, _MyDropdownView.cstml, which uses the helper class you threw the code from the controllers in with something like the following:

    @using MyNamespace.MyHelperClass
    
    @Html.DropDownListFor(model => model.Prop, MyVM as SelectList, "--Select a Property--")

    Then, within your views:

    @Html.RenderPartial("_MyDropdownView")
    

提交回复
热议问题