C #. NET Dynamic binding of drop-down menus in MVC

Source: Internet
Author: User
How to dynamically bind a drop-down menu in MVC
1. Known drop-down menu items:

Enter the following in the Controller class:Code

1 Public   Class Democontroller: Controller
2 {
3 Public Actionresult binddropdownlist ()
4 {
5 List < Selectlistitem > Select1 =   New List < Selectlistitem >
6 {
7 New Selectlistitem {text =   " Content " , Value =   " Value " },
8 New Selectlistitem
9 };
10
11 Viewdata [ " Select1 " ] =   New Selectlist (select1, " Value " , " Text " , " Here is the default value. " );
12
13 Return View ();
14 }
15 }

 

Use in view

1 <% = Html. dropdownlist ( " Select1 " ) %>

 

This method is simple, clear, and convenient. You can use this method if you do not need to read data from the database.

 

2. Read the drop-down list items from the database or array cyclically

The database connection code is omitted here. The data read from the database is similar to the data stored in the string array. The following uses an array as an example.

Enter the following code in the Controller class.

1 Public   Class Democontroller: Controller
2 {
3 Public Actionresult binddropdownlist ()
4 {
5 String [] Texts =   New   String [] { " I " , " II " , " 3. " , N };
6 String [] Values =   New   String [] { " 1 " , " 2 " , " 3 " , N };
7
8 List < Selectlistitem > Select1 =   New List < Selectlistitem > ();
9
10 For ( Int I =   0 ; I < Texts. length; I ++ )
11 {
12 Select1.add ( New Selectlistitem
13 {
14 Text = Texts [I],
15 Value = Values [I]
16 });
17 };
18
19 Viewdata [ " Select1 " ] =   New Selectlist (select1, " Value " , " Text " , " Here is the default value. " );
20
21 Return View ();
22 }
23 }

 

Use in view

1 <% = Html. dropdownlist ( " Select1 " ) %>

 

In fact, this method seems to be similar to the 1st types, but when reading data, a circular statement is used.

 

3. Read all drop-down menu items of a table from the database

Assume that the category class already exists. You can use the category. getlist () method to obtain all types of the table. The table contains two data columns: ID and name.

Enter the following code in the Controller class.

1 Public   Class Democontroller: Controller
2 {
3 Public Actionresult binddropdownlist ()
4 {
5 List < Categoryentiry > Categories = Category. getall ();
6
7 Viewdata [ " Categories " ] =   New Selectlist (categories, " ID " , " Name " );
8
9 Return View ();
10 }
11 }

 

Use in view

1 // First, convert the data in viewdata to selectlist.
2 <% Selectlist categories = Viewdata [ " Categories " ] As Selectlist; %>
3
4 // Then Can be output
5 <% = Html. dropdownlist ( " Category " , Categories) %>

 

Note that the 3rd methods are slightly different from the first two methods used in the view. Of course, you can change the first two methods to the 3rd methods, or change the 3rd methods to the first two methods.

 

Note: The first time I published my studyArticleI am a little excited. When I first came into contact with each other, I encountered problems and posted them to improve my memory. I also hope to communicate with you; if you have other methods, please let me know. In addition, if you have any shortcomings, please point out, thank you.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.