Home > Backend Development > C++ > How to Create a Simple HTML Dropdown List with Static Options in ASP.NET MVC?

How to Create a Simple HTML Dropdown List with Static Options in ASP.NET MVC?

DDD
Release: 2025-01-19 06:34:10
Original
128 people have browsed it

How to Create a Simple HTML Dropdown List with Static Options in ASP.NET MVC?

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>
Copy after login

Then, create a model like this:

<code class="language-csharp">public class PageModel 
{
   public int MyColorId { get; set; }
}</code>
Copy after login

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>
Copy after login

In your view, you can create a dropdown as follows:

<code class="language-html">//  此处应添加使用Html.DropDownListFor()生成下拉列表的代码。  由于原始输入缺少视图代码,无法提供完整的示例。  需要提供模型和视图的更多细节才能完成此部分。</code>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template