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

后端 未结 13 1103
感动是毒
感动是毒 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:20

    Create object with getter for your dropdown values:

    public static class DropDowns
    {
        public static List Items { 
           get
           {
               //Return values
           } 
        } 
    }
    

    Create Razor partial:

    @Html.DropDownListFor(m => "ChoosenItem", DropDowns.Items, "")
    

    Call partial:

    @Html.RenderPartial("DropDownItems")
    

    And finally receive ChoosenItem value in controller. Simply.

提交回复
热议问题