The first case. The view view is loaded with values, and the following steps are implemented
1. Create public list<selectlistitem> XXX (name) {get; set;} in the interface model of the operation
2. Instantiate this.courses = new list<selectlistitem> () in the constructor in the model, and a collection of instances
Note: 1 and 2 are done in the same model.
3. In the controller:
(1) All data is queried from the database (implemented via the interface)
(2) using a foreach traversal to instantiate a variable (type SelectListItem)
Examples are:
var courses = _courseservice.getallcourses (); // querying data from data foreach (var in courses) { new selectlistitem (); // instantiate a variable Li. Text = course. Coursename; = course. Id.tostring (); Model. Courses.add (li); }
4. In the interface
@Html. dropdownlistfor (x = X.courseid (name)// ID to query name
In the second case, the view view is loaded with no value and the following steps are implemented:
There is a difference between the first and only the controller:
Example:
varModel =NewSclistmodel ();//Intermediate Modelvarcourses=_courseservice.getallcourses (); SelectListItem Li=NewSelectListItem (); Li. Text=""; Li. Value=""; Model. Scnames.add (LI); foreach(varIteminchcourses) {Li=NewSelectListItem (); Li. Text=item. Coursename; Li. Value=item. Id.tostring (); Model. Scnames.add (LI);
4. Interface
<tr> Class="admintitle"> <span> course Name: </span > </td> class="admindata"> = model. CourseID, Model.scnames)//id for values </td> </tr>
C # (NOP) drop-down list (with foreign keys)