C # (NOP) drop-down list (with foreign keys)

Source: Internet
Author: User

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)

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.