Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>say we use an enum for the gender:</p> <pre><code>namespace DropdownExample.Models { public enum GenderType { Male=1, Female=2 } } </code></pre> <p>and we make a model like this:</p> <pre><code>using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace DropdownExample.Models { public class ActionModel { public ActionModel() { ActionsList = new List&lt;SelectListItem&gt;(); } [Display(Name="Gender")] public int ActionId { get; set; } public IEnumerable&lt;SelectListItem&gt; ActionsList { get; set; } } } </code></pre> <p>make a controller like so:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using DropdownExample.Models; namespace DropdownExample.Controllers { public class ActionController : Controller { public ActionResult Index() { ActionModel model = new ActionModel(); IEnumerable&lt;GenderType&gt; GenderType = Enum.GetValues(typeof(GenderType)) .Cast&lt;GenderType&gt;(); model.ActionsList = from action in actionTypes select new SelectListItem { Text = action.ToString(), Value = ((int)action).ToString() }; return View(model); } } } </code></pre> <p>then in your view, you use the DropDownListFor html helper you include the following:</p> <pre><code>@model DropdownExample.Models.ActionModel @{ ViewBag.Title = "Index"; } @Html.LabelFor(model=&gt;model.ActionId) @Html.DropDownListFor(model=&gt;model.ActionId, Model.ActionsList) </code></pre> <p>the DropDownListFor html helper uses at leats these two parameters:</p> <ul> <li>the name of the property that will hold the selected value</li> <li>the <code>List&lt;SelectListItem&gt;()</code> that contains all the options in the dropdownlist.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload