Using Html.DropDownListFor() in ASP.NET MVC
In ASP.NET MVC, it is very easy to create a simple drop-down list with static options.
Write a simple Html.DropDownListFor():
Example:
Consider the following example data:
public static IEnumerable<Color> Colors = new List<Color> { new Color { ColorId = 1, Name = "Red" }, new Color { ColorId = 2, Name = "Blue" } };
In your model, create a property that represents the selected item:
public class PageModel { public int MyColorId { get; set; } }
In your view, create a dropdown using the following code:
@Html.DropDownListFor(model => model.MyColorId, new SelectList(Colors, "ColorId", "Name"))
This will generate a dropdown list with "Red" and "Blue" options, and the selected value will be stored in the model's MyColorId property.
The above is the detailed content of How to Create a Simple Drop-Down List Using Html.DropDownListFor() in ASP.NET MVC?. For more information, please follow other related articles on the PHP Chinese website!