从List ASP.NET MVC创建下拉列表

时间:2016-06-28 09:49:57

标签: c# asp.net-mvc razorengine dropdownlistfor

我有一个强类型的视图模型,在一个类对象中,我传递了我需要在@html.Dropdownlist中显示的选项列表

查看模型

 public class CreateCampaign_VM
{
    public MarketingCampaign MarketingCampaign { get; set; }

    public List<MarketingCampaignType> MarketingCampaignType { get; set; }
}

控制器方法

 private CreateCampaign_VM GetCampaignObject()
    {
        CreateCampaign_VM _CampaignObject = new CreateCampaign_VM();


        _CampaignObject.MarketingCampaignType.Add(new MarketingCampaignType {
            CampaignTypeID = 1,
            CampaignTypeTitle = "Email",
            Description = "Email"
        });

        _CampaignObject.MarketingCampaignType.Add(new MarketingCampaignType
        {
            CampaignTypeID = 2,
            CampaignTypeTitle = "Text",
            Description = "Text"
        });

        _CampaignObject.MarketingCampaignType.Add(new MarketingCampaignType
        {
            CampaignTypeID = 3,
            CampaignTypeTitle = "Post",
            Description = "Post"
        });

        return _CampaignObject;
    }

...

 public ActionResult CreateCampaign_Title()
    {
        return PartialView("CreateCampaign_Title_Partial", GetCampaignObject());
    }

Razor视图

@model App.Business.Entities.CreateCampaign_VM
  @using (Html.BeginForm("CreateCampaign_Title", "Campaign", FormMethod.Post, new { id = "CreateCampaignTitleForm" }))
        {
         .....
          @Html.DropDownListFor(// 'MarketingCampaignType object' need help here 
         //...
       }

1 个答案:

答案 0 :(得分:0)

试试这个:

@Html.DropDownListFor(m => m.MarketingCampaign , new SelectList(Model.MarketingCampaignType, "CampaignTypeTitle", "CampaignTypeId"), new { id = "yourElementIdIfAny", @class = "yourClassNameIfAny" })