Create a simple HTML drop-down list using Html.DropDownListFor() in ASP.NET MVC
Question: How to create a basic HTML dropdown in ASP.NET MVC 2 that provides static options? Specifically, I want to provide a choice between "red", "blue" and "green".
Answer:
To achieve this, follow these steps:
Please refer to MSDN articles and usage examples on Stack Overflow.
Consider the following Linq/POCO classes:
<code class="language-csharp">public class Color { public int ColorId { get; set; } public string Name { get; set; } }</code>
Then, create a model like this:
<code class="language-csharp">public class PageModel { public int MyColorId { get; set; } }</code>
Finally, define a list of colors that can be populated from a Linq query or a static list:
<code class="language-csharp">public static IEnumerable<Color> Colors = new List<Color> { new Color { ColorId = 1, Name = "Red" }, new Color { ColorId = 2, Name = "Blue" }, new Color { ColorId = 3, Name = "Green" } };</code>
In your view, you can create a dropdown as follows:
<code class="language-html">// 此处应添加使用Html.DropDownListFor()生成下拉列表的代码。 由于原始输入缺少视图代码,无法提供完整的示例。 需要提供模型和视图的更多细节才能完成此部分。</code>
The above is the detailed content of How to Create a Simple HTML Dropdown List with Static Options in ASP.NET MVC?. For more information, please follow other related articles on the PHP Chinese website!