English 中文(简体)
ASP.NET MVC 3 - Html.DropDownList for Evidated Value
原标题:ASP.NET MVC 3 - Html.DropDownList for Enumerated values

I m New to ASP. NET MVC 3. 我试图在下降的名单上展示一些选择。 这些备选办法将具有奇妙的价值观。 该表有三个价值:

public enum Gender 
{
  Male = 0,
  Female = 1,
  NotSpecified=-1
}

我正试图产生以下超文本:

<select>
  <option value="0">Male</option>
  <option value="1">Female</option>
  <option value="2">Not Specified</option>
</select>

我试图与拉扎尔一道这样做,但损失了一点。 目前,我有:

@Html.DropDownList("relationshipDropDownList", WHAT GOES HERE?)

请注意,我不能ed。 感谢你的帮助!

问题回答

......

//add this to your view model
IEnumerable<SelectListItem> genders = Enum.GetValues(typeof(Gender))
    .Cast<Gender>()
    .Select(x => new SelectListItem
    {
        Text = x.ToString(),
        Value = x.ToString()
    }); 

@Html.DropDownList("relationshipDropDownList", Model.genders)
@Html.DropDownList("relationshipDropDownList", Model.GenderSelectList);

不过,我将使用<条码>DropDownListFor,以避免使用魔,:

@Html.DropDownListFor(m => m.relationshipDropDownList, Model.GenderSelectList);

ViewModel 页: 1 选择性轨道

public static List<SelectListItem> GenderSelectList
{
    get
    {
        List<SelectListItem> genders = new List<SelectListItem>();

        foreach (Gender gender in Enum.GetValues(typeof(Gender)))
        {
            genders.Add(new SelectListItem { Text = gender.ToString(), Value = gender.ToString("D"), Selected = false });
        }

        return genders;
    }
}
    public enum EnumGender
    {
        Male = 0,
        Female = 1,
        NotSpecified = -1
    }


@Html.DropDownList("relationshipDropDownList", (from EnumGender e in Enum.GetValues(typeof(EnumGender))
                                                               select new SelectListItem { Value = ((int)e).ToString(), Text = e.ToString() }), "select", new { @style = "" })



//or

@Html.DropDownList("relationshipDropDownList", (from EnumGender e in Enum.GetValues(typeof(EnumGender))
                                                               select new SelectListItem { Value = ((int)e).ToString(), Text = e.ToString() }), null, new { @style = "" })




相关问题
PHP Combo Box AJAX Refresh

I have a PHP page that currently has 4 years of team positions in columns on the page. The client wants to select the players in positions and have first, second and thrid choices. Currently the page ...

jquery + ajax + json + fill dropdown list not working

I m pretty sure i am almost there....but i cannot figure out how to iterate through json objects and fill a dropdown list. Here is the js code: My JSON data returned:{"name":"County1","name":"County1"...

Gridviews and DropdownLists

Is it possible to change the data source of a dropdown list in a gridview from another dropdown list selected index changed method in the same gridview? for example I have a dropdown that needs to ...

Adding a new value to the drop down list box

I have a drop down list box which has one of the values to be others. I want to move these value to the last. Please help me with this. The code i am using is as follows ddlAssetsCountryOthersone....

Show hide div using codebehind

I have a DropDownList for which I am trying to show a div OnSelectedIndexChanged but it says OBJECT REQUIRED. I am binding the DataList in that div: aspx: <asp:DropDownList runat="server" ID="...

Why might dropdownlist.SelectedIndex = value fail?

I have a dropdown list that I am binding to a datatable. Here is the code I am using to do it: ddlBuildAddr.DataSource = buildings ddlBuildAddr.DataTextField = "buildingName" ddlBuildAddr....

热门标签