ASP. net mvc eight dropdownlist Data Binding

Source: Internet
Author: User

1. In MVC, the control provided by. NET is basically no longer used, but the HTML auxiliary method is used. Therefore, some of the data binding controls we used previously are very different from the current usage,

 

The following describes several data binding controls that we usually use.

 

1. drop-down list box dropdownlist

The homepage shows how to retrieve the data to be bound in the action. The following is an example of my work. You may not be the same as me. It just shows how to bind it.

Public list <selectlistitem> getbypid (int pid) <br/>{< br/> usersmodelsdatacontext umdc = new usersmodelsdatacontext (); <br/> var quert = from V in umdc. cms_kindt where v. PID = PID select V; <br/> List <selectlistitem> item = new list <selectlistitem> (); <br/> foreach (VAR it in quert) <br/>{< br/> item. add (New selectlistitem {text = it. name, value = it. id. tostring ()}); <br/>}< br/> item. insert (0, new selectlistitem {text = "-member type-", value = "-1"}); <br/> return item; <br/>}

 

The above method is to query the member type data through LINQ, and then convert it to the selectlistitem class instance for binding to dropdownlist.

 

Public actionresult memberedit (int id) <br/>{< br/> usersmodelsdatacontext umdc = new usersmodelsdatacontext (); <br/> var query = from V in umdc. cms_membert where v. id = ID select V; <br/> cms_membert Mt = query. first (); <br/> viewdata ["memberkind"] = getbypid (4); <br/> return view (MT); <br/>}

 

The code above is action, and the retrieved member information is used to bind to the page member list. viewdata ["memberkind"] = getbypid (4) is mainly related to dropdownlist ); create a viewdata file to bind the drop-down list.

After the data has been retrieved, it is bound to the view page. The Code is as follows:

 

<Div class = "editor-label"> <br/> member type: <br/> </div> <br/> <Div class = "editor-field"> <br/> <% = html. dropdownlist ("kindid", viewdata ["memberkind"] As ienumerable <selectlistitem>) %> <br/> </div> 

 

The preceding method uses foreach to store row data into the list in sequence, which is nothing more than a hassle. However, this method can insert list items at will. For example, item. insert (0, new selectlistitem {text = "-member type-", value = "-1 "});

If you only bind the database data, you can:

Public selectlist getbypid (int pid, string kindid) <br/>{< br/> usersmodelsdatacontext umdc = new usersmodelsdatacontext (); <br/> var quert = from V in umdc. cms_kindt where v. PID = PID select V; </P> <p> var selectlist = new selectlist (quert, "ID", "name", kindid. tostring (); // dataset, value, name, selecteditem <br/> return selectlist; <br/>}

 

View page: <% = html. dropdownlist ("list") %>

Previous Article: ASP. net mvc 7 construct the HTML page next article: Previous Article: ASP. net mvc 9

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.